backbone-model

How to know when a Backbone model.fetch() completes?

隐身守侯 提交于 2019-12-10 01:50:20
问题 I bind on the change event of my backbone models like this. this.model.on( "change", this.render, this ); Sometimes I want to fetch the latest version of the model and forcibly render the view. So I do this this.model.fetch(); Unfortunately model.fetch() only fires the change event if the new data is different from what was previously stored in the model. How can I always trigger a this.render callback when fetch completes, whether it triggers a change event or not? Thanks (in advance) for

How to know when a Backbone model.fetch() completes?

坚强是说给别人听的谎言 提交于 2019-12-05 01:03:40
I bind on the change event of my backbone models like this. this.model.on( "change", this.render, this ); Sometimes I want to fetch the latest version of the model and forcibly render the view. So I do this this.model.fetch(); Unfortunately model.fetch() only fires the change event if the new data is different from what was previously stored in the model. How can I always trigger a this.render callback when fetch completes, whether it triggers a change event or not? Thanks (in advance) for your help You can use the $.ajax success callback, but you can also just listen for the Backbone sync and

Backbone - not parse each model in collection after fetch

无人久伴 提交于 2019-12-04 19:41:45
问题 How to prevent parse function for model in collection fetching? $(function() { var Task = Backbone.Model.extend({ url : function() { return this.urlRoot + this.id; }, urlRoot: 'index.php?c=light&a=list_tasks_bb&ajax=true&task=', parse: function (response, options) { console.log(options); console.log(response); return response; } }); var TaskList = Backbone.Collection.extend({ model: Task, url: 'index.php?c=light&a=list_tasks_bb&ajax=true', initialize: function () { }, parse: function

In Backbone.js, why do silent changes trigger change events eventually?

只谈情不闲聊 提交于 2019-12-04 08:52:25
问题 When I pass {"silent":true} while setting an attribute in a Backbone model, why doesn't that just suppress the change:attribute event? What is the advantage of firing that event the next time an attribute is changed? Update Backbone 0.9.10 changed the behavior of passing { "silent": true } . From the changelog: Passing {silent:true} on change will no longer delay individual "change:attr" events, instead they are silenced entirely. Browse the changelog here 回答1: This has confused me for some

Backbone - not parse each model in collection after fetch

穿精又带淫゛_ 提交于 2019-12-03 12:51:44
How to prevent parse function for model in collection fetching? $(function() { var Task = Backbone.Model.extend({ url : function() { return this.urlRoot + this.id; }, urlRoot: 'index.php?c=light&a=list_tasks_bb&ajax=true&task=', parse: function (response, options) { console.log(options); console.log(response); return response; } }); var TaskList = Backbone.Collection.extend({ model: Task, url: 'index.php?c=light&a=list_tasks_bb&ajax=true', initialize: function () { }, parse: function (response, options) { return response.tasks; } }); var Light = Backbone.Router.extend({ el: $('#light'), routes

Backbone.js What is the purpose of specifying a model in collection

旧时模样 提交于 2019-12-01 13:29:28
Here is what I am trying to understand. Often times I find myself writing backbone like this: var CallModel = Backbone.Model.extend({ }); var CallsCollection = Backbone.Collection.extend({ model: CallModel, url: 'url/to/external/json' }); It is a very basic example but as you can see, there is nothing really in the model all the data is coming into the Collection via an external url call to a json file that is build from a database. So whats the purpose of the model? I am sure that I am probably not using backbone.js to its fullest extent which is why I am here asking you guys. T J First of

How to force a POST request when saving a model?

半腔热情 提交于 2019-11-28 02:22:25
I need to make a POST to a server-side API. I must send an id key into the request body to the server. I use a Backbone model. But when I do: myModel.set("id", somevalue) myModel.save() The network request that is fired is : URL/someValue [PUT] Backbones doesn't do a POST but a PUT and appends the id to the url. So I just want to pass an id key to the server without Backbone noticing. From Backbone's doc : Backbone is pre-configured to sync with a RESTful API. [...] The default sync handler maps CRUD to REST like so: create → POST /collection read → GET /collection[/id] update → PUT

Nested Models in Backbone.js, how to approach

二次信任 提交于 2019-11-27 16:39:48
I've got the following JSON provided from a server. With this, I want to create a model with a nested model. I am unsure of which is the way to achieve this. //json [{ name : "example", layout : { x : 100, y : 100, } }] I want these to be converted to two nested backbone models with the following structure: // structure Image Layout ... So I define the Layout model like so: var Layout = Backbone.Model.extend({}); But which of the two (if any) techniques below should I use to define the Image model? A or B below? A var Image = Backbone.Model.extend({ initialize: function() { this.set({ 'layout'

Backbone.js get and set nested object attribute

ぃ、小莉子 提交于 2019-11-27 02:34:51
I have a simple question about Backbone.js' get and set functions. 1) With the code below, how can I 'get' or 'set' obj1.myAttribute1 directly? Another question: 2) In the Model, aside from the defaults object, where can/should I declare my model's other attributes, such that they can be accessed via Backbone's get and set methods? var MyModel = Backbone.Model.extend({ defaults: { obj1 : { "myAttribute1" : false, "myAttribute2" : true, } } }) var MyView = Backbone.View.extend({ myFunc: function(){ console.log(this.model.get("obj1")); //returns the obj1 object //but how do I get obj1

How to force a POST request when saving a model?

天涯浪子 提交于 2019-11-26 23:40:17
问题 I need to make a POST to a server-side API. I must send an id key into the request body to the server. I use a Backbone model. But when I do: myModel.set("id", somevalue) myModel.save() The network request that is fired is : URL/someValue [PUT] Backbones doesn't do a POST but a PUT and appends the id to the url. So I just want to pass an id key to the server without Backbone noticing. 回答1: From Backbone's doc: Backbone is pre-configured to sync with a RESTful API. [...] The default sync