English 中文(简体)
• 如何在后台采用固定方法改变违约模式属性。 j)
原标题:How to change default model attributes using set method in Backbone.js

我在《示范公约》中有一些缺陷数据。

window.AppState = Backbone.Model.extend({
  defaults: function() {
    return {
      country:  false,
      region: false
    };
  }
});

var appState = new AppState();

然后,在路线上,我拿着像这样的新价值观:

[["country", "new country value"], ["region", "new region value"]]

And now in a for loop i cycle to update all received params... I realize it this way

appState.attributes[arg] = val;

but I think it s not clear, because in documentation of Backbone.js I read about "attribute" method and see this: "Please use set to update the attributes instead of modifying them directly." So my questions is: How i can change attributes of defaults using set method?

最佳回答

<代码>.set(功能...

appState.set({arg : val});

一旦你使用<代码>.set()提出论点,它将优先于违约。

your example modified a bit:

window.AppState = Backbone.Model.extend({
  defaults: function() {
    return {
      country:  false,
      region: false
    };
  }
});

var appState = new AppState();

// when you get your array of new values you can turn it in an object
var myArray = [["country", "new country value"], ["region", "new region value"]];
var newValues = {};
for (var i = 0; i < myArray.length; ++i)
{
    newValues[myArray[i][0]] = myArray[i][1];
}

// set the properties to the new values
appState.set(newValues);

// if you now return the json of the model, it has changed properties
console.log(appState.toJSON());

remark this for loop depends a lot on the structure of your array, the way i wrote it will work for the array you gave in your example, should anything change to that you will need to change the working of the for loop

remark 2 if you use this technique more often, you could always extract that functionality in a helper method.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签