BackboneJS, create and destroy, but no update method? - javascript

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.

Related

Get edited data of OData Binding

Does anyone know the best approach of getting the changed data of an odata model property after binding it to a form / set of input controls via bindElement?
Currently I am doing something like this.getView().bindElement('/SomeEntitySet(0815)');. But I struggle with getting back the edited data from my view. The only methods the binding is offering to me is getting back the odata model itself, which contains the original, non edited data.
Does anybody know what I'm doing wrong?
In the odataModel there's an Property called refreshAfterChange this should be set to true by default.
Are you sure your changes applies to your backend data? The oDataModel from sapui5 should update the binding by itself(checkout: mParameters.refreshAfterChange).
If the Change method work debug your oData service and check if the change method trigger a GET request to your backend(The odatamodel should trigger a get request if the data has been changed => see refreshAfterChange in link above, the refresh of the model will do a GET request like it does if you bind data to an element). If not theres something wrong with your model confoiguration.
Could you add the code where and how you initiate the model?

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) {
// ...
}
});

Backbone doing POST request instead of PUT request

I am trying to set a model's attribute to be something different. When I save the model, Backbone issues a POST request instead of a PUT request.
I read on another Stackoverflow post that Backbone uses a model's id to determine if a model is new or not. When I console.log(model), the model has an id attribute. When I console.log(model.id), it prints out undefined. How come when I do a console.log(model), the model has a id attribute, yet when I do console.log(model.id), I get back undefined?
Which way does Backbone use to determine if a model is new or not? And, how can I fix the problem? I am using backbone-tastypie.
How did you create the model in the first place? The model's id needs to be specified in the model's data. Usually this would be returned in the server response the first time you save a new object. Once the id is set Backbone will save it with PUT instead of POST.

In Backbone.js, after I 'create' a model under a collection, how do I update that model?

Posts.create({'body':post_body});
When I call that, Backbone will hit my sever with an AJAX post request, creating that post. My server will then return a JSON with the "full" post.
Perfect! But now, I want the newly created model to to have the full data. In other words, I don't want it to only have the body attribute. (all my other models have other data).
My question is:
will backbone automatically update the model with the "full" data because my server returned that full JSON?
if not, how can I get Backbone to update that model so its data is full?
Edit: I did this, and it seems like Backbone automatically uses the data returned as the new model. Can someone confirm?
success:function(post){
console.log(post.toJSON()); //Yay! latest version.
},
Yes, the model will be updated with any additional info your server returns.
This is because under the model's create method, there is a call to save, which intern calls model.set within it, in order to update the model with any amended (or new attributes).
As the source code comment for this method states:
If the server returns an attributes hash that differs, the model's state will be set again.

Categories

Resources