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

How to get a model attribute after fetch operation

本文关键字:获取 模型 属性 操作      更新时间:2023-09-26

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

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

this.model.fetch();

例如,我想获得一个值

this.model.get("VALUE");

如何确保我获得现在从服务器获取的正确值。我正在尝试做类似以下内容的事情,但当然这个.model在整个块中无法识别。

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

我被困在这里。有人有什么想法吗?

好吧,我看到两个选项: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);