繁体   English   中英

bone.js获取模型+ Django

[英]backbone.js fetch model + django

我尝试为django + bone.js创建rest-api,但遇到麻烦,model.fetch()不会更新默认模型值。 我在哪里弄错了?

骨架.js模型

 window.User = Backbone.Model.extend({
     urlRoot: '/user/',
     initialize: function() {
    console.log("init Model User");
     }
     defaults: {
    name: "Default title",
    pass: 2011,
     }
 });

骨架.js视图

window.UserView = Backbone.View.extend({

    initialize:function () {
        console.log('Initializing User View');
        this.model.bind('change', this.render, this);

        this.model.fetch ({
             success: function () {
                 alert('success');
             }
        });
        console.log(this.model);

        this.render();
    },

    render:function () {
        console.log('Print tpl UserView');
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }
});

建立检视

var userView = new UserView({model: new User()});

django响应[{“ name”:“ LOLOLO”,“ pass”:“ 12345”}]

console.log(this.model)

打印

attributes: Object
0: Object
name: "LOLOLO"
pass: "12345"
__proto__: Object
name: "Default title"
pass: 2011

您的console.log(this.model)就在fetch调用之后。

很有可能在记录模型时,响应尚未从服务器到达,因此只能获取默认值。

尝试将console.log调用放入获取的成功回调中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM