Prevent Backbone collection reset event on success callback of fetch - javascript

I call fetch and bass success callback to it. Is it possible to prevent collection reset event in this callback?

Reset is not called on fetch unless you specify it in the options
MyCollection.fetch({reset:true}) will reset your collection but otherwise it won't so I'm not sure what's your problem here.
MyCollection.fetch({reset:false}) is not even documented because reset is not triggered after a fetch. Simple fetch will merge your new results with existing models.
When the model data returns from the server, it uses set to
(intelligently) merge the fetched models, unless you pass {reset:
true}, in which case the collection will be (efficiently) reset.
http://backbonejs.org/#Collection-fetch
Hope it helps.

Related

Loopback - see data before overwriting in "after save" hook

I've seen this question: Strongloop : Compare old model with the new instance in operation hook 'before save'
Is there any way to get the data in the DB before the overwriting is done in the before save hook? Basically, I want to be able to pass one element to update in a put request rather than pass all required fields during the request.
It seems like you are looking for upsertWithWhere or upsert which does a PATCH rather then a PUT. If you do that and the right row can be identified (you send along the primary ID or identify the row) only the passed along data will be updated/overwritten and the rest will be kept.
That's the easy way, if you want to do something more advanced (like doing modifies on DB before a save) then you just have to access the Model in the before save hook and modify. Remember you can do anything before a save as long as you postpone the "next()" call.

How to disable sync event on model's save method in Backbone?

Model's save method by default triggers sync event after the server has acknowledged the change. Is there any way to silence that sync event on model's save?
You can look at the portion of the save method that calls sync in the annotated source:
model.trigger('sync', model, resp, options);
It’s pretty deep in there. You could override the save method to remove that line.
Why do you want to do this?

Change Backbone data after fetching and before change event is fired

I fetch data from the server and want to change some string with I18n strings before the subscribers are notified of the model change. Is there a way to add a success method that will always called in first place after fetching data?
Ok found by myself.
Model.parse
http://backbonejs.org/docs/backbone.html#section-65

backbone model get attribute within initialize method

I've looked for a solution to this but cannot find anything that works.
Note: I am overriding the Backbone.sync method globally.
When I instantiate a Session model I pass a number of attributes to it. These, as you would expect, should be "gettable" via mySessionInstance.get('someAttribute'). The session model calls this.fetch() within the initialize method. My custom Backbone.sync method needs to access some of the session attributes but when it runs I get an error saying the "get" method does not exist for that instance.
It's as if I was trying to access .get for my session instance before it was fully initialized. Any ideas on how to fix it? I wish there was a "ready" or "initialized" event for models so I could just listen for that and ensure certain things only happen when the model is truly done initializing.
Thanks,
Luis
There are two events that a model fetch will trigger:
"change" events will be triggered if anything is changed (i.e. if anything came back from the server). A fetch is mostly a set that is called by an AJAX success handler after all.
A "sync" event will be triggered when the model is synced with the server, syncing includes fetching. The fetch docs aren't explicit on this but you can easily see it in the source and the master events list notes it.
The "change" event is probably more useful in general and should serve to indicate that the model is ready for use.
If you just need a one-time notification, you could use use the success callback from the fetch:
model.fetch({
success: function(model, response) {
// ...
}
});

BackboneJS, create and destroy, but no update method?

I noticed that in BacbkoneJS there is a method in the Collection object that allows you to add a new model to that collection, while at the same time pushing the new model to the Server. This is convenient because it will only add the new model to the collection if the ajax request is successful. It will also update the ID on the model with the one returned from the server.
There is also a similar method on the Model object called destroy, this will only destroy the model if the ajax request returns 200.
How can I achieve something similar to this with Update, where when I set data on my model, it will attempt to save the model to the server, ensure a 200 status, and then fire the "change" event?
The problem is If I set the model data (I have to do this in order to call save()), then the change event fires.
Thanks
The Backbone.Model.save() method allows you to specify the properties you want to change. Looking at the annotated source code, it looks like the "model.set()" happens on a successful response from the server.
So, instead of doing
myModel.set({this:"that"});
myModel.save();
Do
myModel.save({this:"that"});
and the change event should fire after a successful save.
Note: I have not tested this - this is just from reading the source.

Categories

Resources