Backbone.js 模型

发布于 2024-12-27 08:21:18 字数 810 浏览 3 评论 0原文

我在这里做错了什么吗?我正在从 jsonp 响应填充骨干模型。当我在 firebug 中查看第一个控制台语句时,“当前”对象在属性下有“图标”。但是,当打印 console.log(current.has("icon")) 时,它会返回 false,因此 current.get("icon") 将返回 undefined。

var Current = Backbone.Model.extend({
    url: "A_valid_url",
    sync: function(method, model, options) {
        options.timeout = 10000;
        options.dataType = "jsonp";

        return Backbone.sync(method, model, options);
    },
    parse: function(response, xhr) {
        return response.current_observation;
        }
});

var current = new Current({test: "blah"});
    current.fetch();
    console.log(current);//under attributes there is a "icon"
    console.log(current.has("icon")); //false
    console.log(current.get("icon")); //undefined
    console.log(current.has("test")); //true

Am I doing something completely wrong here? I am populating a backbone model from a jsonp response. When I view the first console statement in firebug, the 'current' object has 'icon' under attributes. However, when printing console.log(current.has("icon")) it returns false and therefore current.get("icon") will return undefined.

var Current = Backbone.Model.extend({
    url: "A_valid_url",
    sync: function(method, model, options) {
        options.timeout = 10000;
        options.dataType = "jsonp";

        return Backbone.sync(method, model, options);
    },
    parse: function(response, xhr) {
        return response.current_observation;
        }
});

var current = new Current({test: "blah"});
    current.fetch();
    console.log(current);//under attributes there is a "icon"
    console.log(current.has("icon")); //false
    console.log(current.get("icon")); //undefined
    console.log(current.has("test")); //true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2025-01-03 08:21:18

fetch() 是异步的。请尝试以下操作:

var current = new Current({test: "blah"});
current.fetch({
    success: function(){
        console.log(current.get("icon"));
    }
);

fetch() is asynchronous. Try the following:

var current = new Current({test: "blah"});
current.fetch({
    success: function(){
        console.log(current.get("icon"));
    }
);
岁月蹉跎了容颜 2025-01-03 08:21:18
current.fetch();
console.log(current);//under attributes there is a "icon"
console.log(current.has("icon")); //false
console.log(current.get("icon")); //undefined
console.log(current.has("test")); //true

这是真正的代码吗?如果是这样,所有这些日志记录将在数据的异步加载完成之前发生。您需要在回调中执行此操作(或绑定到模型上的“更改”事件)。

console.log(current);//under attributes there is a "icon"

数据仍然显示在控制台日志中是一个“功能" :它只会传递引用,实际打印稍后发生(当数据可能已经加载时)。

current.fetch();
console.log(current);//under attributes there is a "icon"
console.log(current.has("icon")); //false
console.log(current.get("icon")); //undefined
console.log(current.has("test")); //true

Is that the real code? If so, all this logging will take place before the asynchronous loading of the data has completed. You need to do this in a callback (or bind to the "change" event on the model).

console.log(current);//under attributes there is a "icon"

That the data still shows up in the console log is a "feature" of the log when you log objects (instead of strings): It will only pass a reference, and the actual printing happens later (when the data might already be loaded).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文