Dynamically calling show() or attachView() with Marionette - marionette

Currently, I'm rendering html server-side for my view and using attachView(). However, if that view is closed and later reopened, it seems I need to use show() to re-render it client-side. Also, when instantiating the view (and its subviews) they need to have the $el provided or not.
My thought is to (within the controller) check whether the html element exists in the dom, and dynamically call attachView() if it does or show() if it doesn't.
It seems there has to be a better way and that I'm probably missing something. Does anyone have an elegant way of handling this, either similar to what I've described or entirely different?

Related

Can a view component call its own methods via ajax after rendering within the parent page?

Maybe I am missing the point, but if you have a ViewComponent, the examples ive seen so far, all do their work within their 'InvokeAsync method, where they are passed a model and return a view.
If the view contains a databound control and you need to bind to data via Ajax, where can those methods be, within the ViewComponent or the parent page?
Ok, so take this example
https://github.com/pkellner/progress-telerik-blog-viewcomponent/tree/master/WebApp/Pages/Components/RatingControl
Can the viewcomponent be used for helper methods that are called from its own view, eg with Ajax loading. Same question goes for other controls as well, eg a DataGrid within a viewcomponent, where would the variosu crud helper methods go?
Well, it finally dawned on me that NO you can not do what was suggesting. Per the docs, a ViewComponent (as least as of 3.0) do not respond directly as an http endpoint.
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.0
Solution - make an http endpoint, eg. a web api, which could be called via jquery/ajax from the markup in the viewcomponent.

Is there a way to implement "onShow" using Prototype JS?

I searched the Prototype docs but found nothing. Did I miss something, or is there no such thing as onshow? I want to call a function when an element (a div in my case) is made visible. Is there such functionality in the Prototype framework? If not, I need a push in the right direction so I can solve it another way.
Thanks!
If you are asking if there is an event that is fired when the .show() method is called on elements there is not.
But you can test if an element is visible by using the $('myelementid').visible() method.
Also a different way I have done it in the past is using the Script.aculo.us .appear() method which has a afterFinish callback - for example
$('myelementid').appear({'afterFinish':function(effect){
//the afterFinish callback passes the effect as the first parameter
//call other function with element
otherfunction(effect.element);
});
technically you would only need to load the effects.js file of Script.aculo.us instead of all of the library to get the core effects methods.

Refreshing Partial View in MVC 3

I have a partial view that I have included on my _Layout.cshtml. It simply has a javascript function that changes an image based on the state of my system. I don't need to reload any data, I don't need to go to the code of the controller for anything, I simply need to reload that partial view.
I tried many of the examples that I found here but couldn't get any of them to work. I felt as if they were too complex for what I was doing anyway. Any guidance would be appreciated.
Thanks,
Steve
If the partial is loaded into the layout directly then there's no straightforward way to refresh it, because it's basically a part of the complete rendered page.
Your best bet is to render the partial using $.load or whatever equivalent you have available by hitting a controller method and rendering the result into a container (like a div). You would have to do this within a script that is loaded with the layout itself, by observing document.ready or something like that. Once you have that in place then it's trivial to keep reloading or refreshing the contents by hitting the controller method as many times as you need. For example in jQuery:
$(document).ready(function () {
RefreshPartial();
window.setInterval(RefreshPartial, 10000);
});
function RefreshPartial() {
$('#container').load('/some/controller/endpoint', {parameters});
}
This will call the controller method, and set the inner contents of the element identified with #container. You can call RefreshPartial as many times as you want.
Partial views only exist on the server. The only way to "refresh" the partial is to go back to the server to get it again.
Obviously, you must be doing something in the partial that needs refreshing. Whatever that is, should be callable from javascript to do the refresh.

Simple way to call a method on the View (Code Behind)

I have a small issue I was hoping somebody could help me with. I have to call the NavigationService.RemoveBackEntry() on two of my views due to the way I have my first run wizard set up.
This method needs to be called on the view (in the codebehind) as far as I am aware and cannot be called in my view models.
I was wondering what would be the easiest, cleanest way to call a RemoveLastNavEntry() from the ViewModel if the method lives on the view.
Rob has said it is a feature he will build into the navigation service at some point but until then I need to implement this as a minor hack.
While this truely is a task for the view, you can, if you really want to, call it from the ViewModel, as a static call.
(App.Current.RootVisual as PhoneApplicationFrame).RemoveBackEntry()
See PhoneApplicationFrame.RemoveBackEntry Method for documentation.

Use Drupal7 AJAX goodness programmatically

X post from http://drupal.org/node/953016
The Drupal 7 AJAX system is great, it works very smoothly for forms and even for links.
What I can't work out how to do in a sane way is to call it from javascript. I may want to have a dynamic page without a form and as part of that make a Drupal ajax call, specifically so that the ajax commands get run on return.
The most effective way I have found to do this so far is:
dummy_link = $('Loading Vars');
$(vars_div).append(dummy_link);
Drupal.attachBehaviors(vars_div);
dummy_link.click();
Which is effective but a huge hack. I havn't found a way to perform an ajax call and have the Drupal ajax framework do it, rather than the standard jquery framework.
I would have thought that it was possible to invoke the drupal ajax api directly, does anyone know how?
The short short answer is you'll want to get yourself to something like:
$.ajax(ajax.options);
Which is the jQuery part, but with a set of options that help you hook into the Drupal Goodness in terms of success handling, effects, etc. This is what is effectively what's hapening for you in your "huge hack" example.
Creating a new Drupal.ajax function programatically still requires a synthetic element:
base = 'someid'
element = $('Loading Vars');
element_settings = {'url': uri, 'event': 'click'}
myAjax = new Drupal.ajax(base, element, element_settings)
But you can at least trigger it without simulating a click in the UI:
myAjax.eventResponse(element, 'click')
It feels like there should be a better way to do this, but it requires another way to set up the initial ajax prototype that doesn't require a DOM element. Because so much of the interaction-set hinges on how to move data back into the DOM, I don't think this use-case is well-supported yet.
It may also be possible to go direct to jQuery with a proper set of options and get the effect you want, but the Drupal.ajax protoype functions self-refer quite a lot so doing it without a Drupal.ajax class seems dicey.

Resources