如何在获取操作后获取模型属性

时间:2013-09-26 14:50:21

标签: javascript html5 backbone.js backbone-views

我是骨干的新手。我想知道当我执行以下操作时如何获取模型获取的值。

例如,如果我做了类似下面的事情

this.model.fetch();

我希望得到一个例如

的值
this.model.get("VALUE");

我如何确保获得正确的从服务器获取的值。我正在尝试做类似跟随的事情,但当然这个模式在完整的区块内无法识别。

    this.model.fetch().complete(function(){
        window.localStorage.setItem("VALUE", this.model.get("VALUE"));
    });

我被困在这里。有没有人有任何想法。

1 个答案:

答案 0 :(得分:-1)

嗯,我看到两个选项:1。只需删除完整的块并单独使用这些功能。

this.model.fetch();
var value = this.model.get('value');
//or, if you want all of the values
// var values = this.model.toJSON();
// values.value -> the specific value

我对本地存储不是很熟悉,但为什么要获取值然后将其设置为本地存储?

或者,2。您可以将内部语句放在函数中,并将其绑定到this

initialize: function () {
    _.bind('setItem', this);
},
setItem: function() {
    // assuming this code works
    window.localStorage.setItem("VALUE", this.model.get("VALUE"));
}
// elsewhere, and not familiar with complete, so I'm not certain how this works
this.model.fetch().complete(setItem);
相关问题