In Backbone.js, what is the opposite of collections.fetch()? - javascript

http://documentcloud.github.com/backbone/#Collection-fetch
According to the doc, "fetch" hits a url and GETS data.
How about POST? What if I want to CREATE or UPDATE something? What do I do ?

The "opposite" is save.
model.save([attributes], [options])
Save a model to your database (or alternative persistence layer), by delegating to
Backbone.sync. The attributes hash (as in set) should contain the attributes you'd like to
change -- keys that aren't mentioned won't be altered. If the model has a validate method,
and validation fails, the model will not be saved. If the model isNew, the save will be a
"create" (HTTP POST), if the model already exists on the server, the save will be an "update" (HTTP PUT).

Related

Sending a variable attribute every Ember Data request

I have various Ember models which all need to send the same attribute to the server to identify which information they should modify, I know I could add a non-serialised attribute to each model and set it each time, but that seems messy.
The attribute is on the Application controller, and I want it to be sent whenever I save/get/update/etc a model.
Any ideas on how I might do it?

Backbone model sync status

In my app, the user creates a collection with a bunch of models. As I don't want to make a ton of requests to the server, I've created a custom function that saves these models in batch to the server. The server then responds with all the models including their id, and this is then set to the various models. All working well so far.
The problem I'm having now is that Backbone doesn't know that at this point all models are synced with the server. So at a later point in the app, when I call model.save() on each model, it sends each model to the server again (which should be only the ones that are changed since the batch operation). How could I let Backbone know that all models are synced? I was looking at the 'changed' and 'hasChanged' attributes, but am not quite sure if I should manipulate these (I guess not).
Backbone does not include the feature of tracking changed attributes since last sync with server.
The changed and hasChanged are not dealing with changed attributes since last sync with server.
You will have to create your own mechanism for tracking the state of your models:
Each model should have a hasChangedSinceLastSync flag.
Each model should bind (.on) to it's own change event and set the flag to true.
Override the sync method and set the flag to false once data is returned from the server (for read/create/update).
Backbone will make a HTTP POST to create your model server-side if the id attribute of you model is unset/null.
So what you should do is set this id attribute manually when the server returns them.
Also, if your id attribute is not id, you can tell backbone that it's another attribue.
Cf, the docs :
Manually set an attribute : http://backbonejs.org/#Model-set
The id attribute of your model : http://backbonejs.org/#Model-id
Reference to the sync method that is being called under the sheets when you save() a model : http://backbonejs.org/#Sync

Backbone.js - id vs idAttribute vs cid

I've been studying Backbone.js for a few weeks, and I feel comfortable using views with models, routers, and collections.
I still have some big gaps:
What is the connection between id, cid, and idAttribute? How do they affect each other?
When exactly does a new model get its ID? Is the server responsible for assigning it? Do I need to add it to the defaults of the model (maybe as a function)? Maybe the addNewModel function should do that?
What is the connection between id, cid, and idAttribute? How do they affect each other?
Both the cid and id should be unique id's for the model, and can be used to retrieve a model from a collection.
The difference between the two is that the cid is assigned by backbone.js client side and is useful if you don't have an actual id, either because the model hasn't been saved yet to the server or perhaps you aren't even saving it to a db (maybe you're using localStorage). The id attribute should be the id of the model that comes from your server (that is what the id is in your db). idAttribute tells backbone which "field" coming from your server it should use to update the id attribute, by default this is set to "id" but as it says in the documentation if your server uses something else you can set it to that (the example given is setting it to "_id".
When exactly does a new model get its ID? Is the server responsible for assigning it? Do I need to add it to the defaults of the model (maybe as a function)? Maybe the addNewModel function should do that?
They should get the new id's when saved to the server and you shouldn't need to set it manually (based on the idattribute) unless you need more control over the process.
id - id that might be manually set when the model is created, or is populated when model has been saved on the server (see "idAttribute" at the bottom to see the connection). This is the id that is sent to the server when model is loaded or updated from server e.g., for a model Person this call will be made if id is 123, "/person/123"
cid - unique id set my backbone model for internal use
idAttribute - this decides which property will act as the unique id (default is "id") when the model has been saved on the server e.g., a model's unique key on the server might be defined by "personId", so when fetch is called model will map the server response from "personId" to id in the backbone model.
id is server model id, cid is client id.
server model: such as Rails Model
client model: backbone model
The id property on a model is automatically assigned based on the id set in the model’s attributes hash. Ideally, this is the ID that you receive from the rest API for the resource that you are querying. On the other hand, cid is an ID temporarily assigned to each model and is useful until an actual ID is determined for the object. For example, a model pushed to a collection that has not yet been persisted can be addressed using cid, until it is saved in the database and an actual ID is generated for it.

Direct store and taking ID from server response while add action

There is the store with direct proxy. I want to create new record. Direct proxy sends it to server and recieve sended data width NEW ID. I want to insert this ID in the grid, but...
Added record shows for a moment and then It become nulled (all field are null). I see null id and nothing more. How to update that record (or do any action) to see it in the grid correctly with new id?
I don't think you need to anything special to insert record to the grid. The way ExtJs base store/proxy classes work is this - when store send update to the remote server it will try to parse new records from the response. And it should automatically replace existing records in your store object (which to this moment will already have new record, but without Id and with phantom = true).
So you need to make sure that response you're receiving from the server does contain new record and that your proxy is configured properly to parse it from the response.

ID for a newly saved backbone.js model

Backbone.js makes a POST request when a new model is created and saved, but it doesn't consider the model to be saved (i.e, further saves result in PUTs not POSTs) until the model has an id.. how should the server return the ID of a newly created model so that backbone.js can set it (i.e, how should it respond to the initial POST)?
If backbone.js doesn't handle that, I assume the best way to do it is using the success handler to set the ID?
You have two options. The first is to return the same JSON structure for a POST request as you would a GET request for the show action (returning a single item.) This uses a single request.
From the documentation:
Set a hash of model attributes, and sync the model to the server. If the server returns an attributes hash that differs, the model's state will be set again.
The other option is to trigger a fetch on your collection after you save. This will take more than 1 request though and will always be less efficient.

Categories