I am using backbone 1.0.0, and reading here that after a fetch
backbone collection fetch doesn't fire reset()
a collection set is been called to "smart" update collection
which event is been triggered after the "smart update" I tried "change" but didn't worked.
I had to call the fetch with
{reset: true}
and use the "add" event but I don't really want to reset the collection.
Calling fetch() also calls set() once the data returns, which then fires a "smart" update. This smart update should trigger a change event. Furthermore, this behavior should exist in 1.0.0.
Can you show us the code?
A 'sync' event is fired after a successful fetch(). I understand 'reset' is fired before the fetch().
Related
The main point of the question: I need the event that fires when the new rows have been rendered and the selection has been restored.
I refresh my grid by calling store.load method. I have a handler for store.load event. It fires when data have been loaded, but before new rows rendering.
I tried afterrender, viewready, selectionchange both in Ext.grid.Panel and Ext.view.Table (the view of the gird), but these events don't fire after every store.load event.
I need this event to work with the restored selection and perform some operations.
instead of calling the load call doRefresh() it will fire beforechange event and change events on the paging toolbar then handle those things in those events do what ever you want todo
you can use:
grid.getView().refresh();
method after store load. It will refresh grid view content.
I did some reading and learned that fetching a collection triggers the reset event for the collection and change event for existing model that changed.
In my backbone app, I fetch a collection and various relations (pages -> partials -> variables). But when I do, the change event is triggered for partials and variables, which is not what I want, they are only loaded.. Not changed!
Am I doing something wrong or is this default behavior?
After some digging in the source, I found out it does. It does a change:[key] event.. I added silent:true to my fetch, so it skips the event triggers.
Simple question: What is the best way to fire an event 'only once'?
I have a Collection in backbone with multiple models. Sometimes multiple models' attributes are updated at once, firing multiple 'change' events on the Collection.
What is the best way to fire the 'change' event only once, grouping all attribute changes together?
My best idea at the moment is using a timer but this will only capture the first model attribute change.
All suggestions, ideas and solutions are valued. Thank you :).
Context:
In this case the event fires a very computationally intensive function, therefore multiple runs must be avoided.
Whenever you are updating the attributes on the models in bulk, pass {silent:true} as an option, which will suppress any events from being fired.
Then trigger a custom event for which a view or multiple views listening to the same.
You can "debounce" the method in your collection that responds to change events, though you'll have to manually figure out which models changed:
Col = Backbone.Collection.extend({
modelChanged: _.debounce(function() {
// handle model changes
}, delayInMs),
initialize: function() {
this.collection.on('change', this.modelChanged, this);
}
})
You can try resetting the collection, rather than adding the elements. See Collection reset method description:
reset collection.reset([models], [options])
Adding and removing models one at a time is all well and good, but sometimes you have so many models to change that you'd rather just update the collection in bulk. Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event at the end. Returns the newly-set models. For convenience, within a "reset" event, the list of any previous models is available as options.previousModels.
As documentation says, if you use reset, instead of add you will get a single reset event in the end.
I am developing application using ExtJS 4.1. I have one spinner field and I want to change value of that method programatically. I have set up listeners like change, spinup and spindown for this same spinner field.
Now I would like to know how to prevent listener method of these events getting fired only when I change the value of spinner field through my program?
For example,
var mySpinner = Ext.ComponentQuery.query('#foopanel > #mySpinner')[0];
mySpinner.setValue(2000);
When mySpinner.setValue(2000); line is executed, change event of mySpinner gets fired and as I have listener method for this change event, that listener method is executed.
Is it possible to prevent invocation of change event listener method?
Thanks..
You could suspend all events by calling
mySpinner.suspendEvents();
mySpinner.setValue(2000);
mySpinner.resumeEvents();
That would be the cleanest an easiest way IMO
And that's also a usecase why this methods exist
I have a Backbone Collection initialized by running collection.fetch() method, and then after a while, I asked the collection to fetch again in order to refresh the models. so, is there any event from Model fired that I can subscribe from View in order to remove/update the view.
There isn't a specific "the collection has been refetched" event but you don't need one. fetch resets the collection:
fetch collection.fetch([options])
[...]
When the model data returns from the server, the collection will reset.
And reset triggers a "reset" event:
reset collection.reset(models, [options])
[...]
Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event at the end.
So just listen for "reset" events from the collection and re-render the view when you get one.
The behavior of fetch changed in Backbone 1.0, from the ChangeLog:
Renamed Collection's "update" to set, for parallelism with the similar model.set(), and contrast with reset. It's now the default updating mechanism after a fetch. If you'd like to continue using "reset", pass {reset: true}.
And if we look at set:
set collection.set(models, [options])
The set method performs a "smart" update of the collection with the passed list of models. If a model in the list isn't yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren't present in the list, they'll be removed. All of the appropriate "add", "remove", and "change" events are fired as this happens.
So you can say collection.fetch({ reset: true }) if you want to keep using the "reset" event or you can collection.fetch() and listen for individual "add", "remove", and "change" events.