lore-models
AJAX abstraction tier for Lore.
Interface
The interface for this library is designed to mirror Backbone.Model and Backbone.Collection with the following modifications/notes/exceptions:
- This library should not be used to pass data through your application. It is intended solely as an AJX abstraction tier. JSON data goes in, a request in made, and the response is serialized back into JSON and then emitted from an action for the reducers to pick up.
- All event emitters have been removed. Again, this is purely an abstraction tier, not a way to store your data.
- The jQuery.ajax has been removed in favor of something much lighter weight that also facilitates testing in a non-browser environment (i.e. Node).
var Model = Model;var sync = sync;var _ = ; // The .extend() method is something that allows you to define common functionality in// one model (like common parse methods or url buidling logic) and then build other// models from that so you don't have to duplicate the code.var Todo = Model;
Methods
model.fetch(options)
Fetches the model. So assuming the model has an id that was passed in during construction, it will call http://localhost:300/api/todos/:id
to retrieve the model data.
model.save(key, val, options)
This will either become a PUT (update) or POST (create) request depending on whether the model has an id specified. If you simply call mode.save()
it will send all data attributes (model.attributs) to the server. If you pass in arguments, it will send only those attributs to the server (which is useful sometimes when certain API servers interpret the absence of data as being actionable, or throw errors when you pass in data you can't update instead of running it through a whitelist and ignoring it).
model.destroy
Will send a DELETE http://localhost:300/api/todos/:id
request to the API server.
Response Object
This library uses Axios to make AJAX calls, which has a response schema that something like this:
var response = config: headers: Accept: "application/json, text/plain, */*" method: "GET" parse: true responseType: "json" url: "http://localhost:1337/todos/91" data: id: 91 title: "Update README" createdAt: "2016-07-02T00:00:06.407Z" updatedAt: "2016-07-02T00:00:06.407Z" headers: {} {} status: 200 statusText: "OK";
So when you make a call like this:
var todo = id: 91 todo
the response object in the promise looks like the example above