Backbone.js - user collection events

I am trying to implement custom collection events to bind a view.

In my opinion, I want to do something like:

this.collection.on('available', this.available); 

And somehow call this in a method inside my collection.

So to speak, I have a method in my collection that sets the attribute of certain models (available), how can I then run all the views associated with this method?

Is it possible, and the ability to transfer the model in question to the view for updating.

Thanks for any help in advance, really appreciate :)

+4
source share
1 answer

It is very simple to add new events to Backbone. You just need to call the trigger method on the object for which you want to enable the event.

For example, if you are in a collection method and have a model (called model ):

 this.trigger('available', model); 

The code for binding to the available event will be as you described in your question.

EDIT: Backbone currently provides a listenTo method that you usually should use when binding to collection events from your views. Views are automatically untied from this event when the delete function is called, which stops old views from continuing to receive collection events after they are deleted. From your point of view, this can be used as follows:

 this.listenTo(this.collection, 'available', this. available); 
+6
source

Source: https://habr.com/ru/post/1435031/


All Articles