Change Backbone data after fetching and before change event is fired - javascript

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

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?

Prevent Backbone collection reset event on success callback of fetch

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.

What exactly happens when you save a Backbone model?

What exactly happens when you save a Backbone model? Here's the best I can piece together by reading the documentation here:
model.save([attributes], [options]) is called
A "change" event is fired (but only if the attributes are new)
The server is notified of the change?
A "sync" event is called once the server returns
But I'm a Backbone noob and I'm sure someone else could do a way better job of explaining.
I'm partly just curious what happens. I'm also having trouble understanding how Backbone comes up with the JSON object it sends to the server. I'm having a separate problem where the JSON object is not what I want it to be, but I don't know how to change it.
The detailed process can be found in the annotated source code for Backbone.Model.save and Backbone.sync.
If you ignore options.wait and options.silent, your decomposition is mostly correct.
When you issue a model.save:
the attributes passed to the function are set, a change event is fired if the values changed
save delegates the request to model.sync or Backbone.sync
sync serializes the data to a JSON string by calling JSON.stringify(model.toJSON())
An Ajax request is sent to sent to server, a POST request for a new object, a PUT for an update. The target URL is defined by model.url (or collection.url/id)
When the request completes, the model is updated with the server response, if any, and triggers a change event accordingly.
Success or error callbacks are called, a sync event is triggered if no success callback is defined.
Usually, you can customize this behaviour by overriding model.toJSON or model.sync
first,I suggest you read the source code of the backbone, is really very simple.Default backbone and server-side interaction is achieved through backbone.sync.
second,You can trace debug model.save method of code again, naturally know the details.
I suggest you start hereļ¼š
http://backbonejs.org/examples/todos/index.html

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.

FullCalendar: Fetching the initial date/time-stamp to show once events are fetched from server

First of all, hats off to Adam for pulling off this wonderful, well-written calendar plugin!
Now for a a very typical use case. Below is what I want to achieve:
Fetch events (URL added to eventSources) from the server side as JSON data. This is all fine and I have been able to achieve this. The events get rendered properly.
USE CASE in question: Once the fetch is completed and BEFORE the events are rendered and shown on the browser, I want to 'fetch' the initial month/date to show to the end user. I was thinking of a separate AJAX request to fetch the timing details from the server side and then use 'gotoDate' to switch the view's date.
The reason for this specific requirement, being that that the end user would like to see all the events and initial view w.r.t the server time. In our application it is possible that the client and the server box are not at all sync'd w.r.t time.
Unfortunately, as yet I am not able to locate any callback method that gets invoked once the event fetch (all relevant events) is complete and 'before' the events get rendered on the view.
Any help here will be appreciated. Please let me know if any further info is required.
Thanks,
Mohit
"events" - can process a single event source, this could be an array / JSON feed or function.
"eventSources" - is similar except that it expects multiple event sources, these can also be an array of arrays/functions/JSON feeds (anything that the events option would take).
To process the data before it is rendered I expect you could use a function to do some post processing after you have fetched your data with an AJAX request. See e.g.:
http://arshaw.com/fullcalendar/docs/event_data/events_function/

Categories

Resources