Am I using marionettejs properly? - marionette

hi i've been using backbonejs for almost 2 years now and am just starting out on marionettejs with my first app at this website.
the reason for my question is that in my app i have a Layout with a 2 composite view rendered as dropdown list and table (which updates itself whenever we change the selection on the dropdown list).
what i did was for the "change" event on my dropdown list, i have an MyApp.vent.trigger() which i have a listener at the MyApp.addInitializer() function that updates the other compositeview (the table below the dropdown list). actually for the whole app i have almost 6 of this triggers inside itemViews and compositeViews and listeners are inside the addInitializer() for some of the other functions.
i just want to know if i did this right? or is this how dev in marionettejs normally is?
thank you

Yes, that's the recommended idea. However, you don't necessarily need to use the top-level vent attribute: each sub-app and view has their own (scoped) event manager. In addition, you don't necessarily need to add the listener in an addInitializer.
Here's an example (from my Marionette book):
trigger an event at the view scope: https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_view.js#L15 This uses the triggers hash, but you could also do this.trigger("my:event") from within the view, like here: https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_view.js#L29
listening to the events (and retriggering them if necessary): https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L48 and https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L38
Hope this helps !

Related

How to add to an ObservableCollection from a different view?

(I'm using Prism Dryloc) My application consists of two views. The first contains a single list view displaying strings and the second - an entry and a button.
The listview is bound to an observable collection in the first page's View Model. How can I add to the observable collection from a different view?
Great question! You're essentially trying to pass data between views, and there's several ways of passing data between Views in Xamarin.Forms.
The two ways that seem relevant for your case:
Either making the ObservableCollection a public static object (so there's only one global instance of it). Not recommended.
The better way is to use the messaging center so that the second page publishes an event when the button is pressed, that the first page is subscribed to. And it passes that information that gets added to the list.
If these don't work, elaborate your use case and I'll suggest some more

Titanium: correctly clean up a controller associated with a non-window view

I have an Alloy app structured with a one window divided in two views: a sidebar menu view on the left and a main view on the right.
Each time the user click on a different menu-item in the sidebar I remove all children attached to the main view and load the view/controller related to that menu-item and attach it to the main view. My question: is this sufficient? If the removed view had event listeners attached to elements inside it, are they clean up too or not?
Have I to keep a reference to each controller I have instantiated and call controller.destroy() and controller.off() before I load the new view/controller or is not necessary?
I am asking because with views without window is not possible to attach a cleanup function to the "onclose" event because the UI.View elements can't be "closed" (only windows).
So which is the correct way to handle this situation in Titanium?
The event listeners may keep a reference especially you add them with Ti.APP.addEventListeners.
So, you should remove event listeners manually when you remove all children view.

Backbone.js Use View to Manipulate Another View

My question is mostly conceptual about Backbone.js, but I can mock up some code if my question is unclear.
Consider a case where I have 2 sections on a website. A list of items as one view and another view that has a dropdown to select how the list of items should be sorted. Obviously, the list of items is associated with a collection of models that stores the actual data that populates the list. But I'm unsure the best approach for triggering the collection to be sorted differently when the other view's dropdown changes. Should I be changing the actual order of the collection, or just render the view in the order that I want in the view?
Also, is it a good idea to use a model for the dropdown to keep track of the state of the dropdown, and bind the list of items view to that model so that I know when to rerender the list of items?
You could take several roads here. Here's a few:
Use a router. The router would hold your views, or at least the top-level view (cleaner), and your dropdown view would trigger a route change, which would pass the information along to the view. Best if you want clean URLs.
Make a pointer to the list view in the dropdown view. When the dropdown view receives the change event, it explicitly tells the list view to update. (IMO a terrible approach, but listed here for completeness.)
Back everything with models. (Like you suggest in your last question.) The dropdown view would be backed by a model, and the list view could bind to that model's events. (Again, the list view still has to know about the dropdown model—not ideal.)
Make an event manager. When your dropdown recevies a change, you trigger 'sort' and anything that cares can listen to that event. This isn't a trivial solution but isn't overly complex either. Your list view could then register its intent to listen to the sort event with the event manager, thereby abstracting the actual view inside the event manager and away from the dropdown view.
1 & 4 are effectively the same thing, just depending on whether you want a router or not.
Basically my heuristic with these sorts of scenarios is "nothing should know about things it doesn't need to know about." Applied here, that means that your views shouldn't know about each other.

Persist Data Between Actions when using Action Stack in Zend

I am designing a web application with a left sidebar. Each of my actions has its own widgets which must be loaded into the sidebar, and in some cases multiple actions are stacked using the Action Stack plugin. When all actions have finished running I need to 'normalise' and manipulate the number of widgets, then render them.
At this stage I am thinking that:
Each action should store its list of sidebar widgets with the view, and
A front controller plug-in with a dispatch loop shutdown event should take the list from the view and operate on it
Does this sound reasonable? Is there a better way?
I wondered whether I should be storing the list of sidebar widgets with the response object directly, but I don't think this object allows user variables, does it?
Your thoughts are much appreciated! Yeehhaa!
Ok, so I ended up defining a list of widgets in an array for each controller, then modifying it where required in each action. But now I am in the process of moving more logic into the model, so no doubt will all change.
I have been reading about the evils of the ActionStack and now that I have my head around how to go about things without the action stack, life is much simpler!

Notifying other page database item has changed, best practice for MVVM WP7.5?

First of all, I'm using MVVM for this and I would like to continue using it and ofc using the best practices as well.
I have a Mainpage which will display a list of items. On another page you can add such items. The items will be saved in a database which came with the Mango update.
When a item is added I want to navigate back to the mainpage and I want the list to be updated automatically. Is this possible and what's the best way? I'm thinking of following scenario's:
Use the Refresh query string when you navigate. Check in the back end of your main if there is a refresh. Then send a message 2 the ViewModel that he needs to update his list. I have tried this and this works. But this doesn't really sound the right way for MVVM.
Can't this be done with the NotifyPropertyChanged event that you can raise on your Database Model ? Or doesn't it work over different pages?
Reload the whole ViewModel for the main page somehow.
Any other idea's?
Use MVVM-Lights Messenger. The MainViewMode can subscribe to a Refresh event and the ViewModel where the items are added can publish a Refresh event.
This is a good example of how messenger can be used.

Resources