主干模型保存和回调问题 [英] backbone model saving and callback issues

查看:230
本文介绍了主干模型保存和回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Backbone。我有两个问题要根据下面的代码。

I just start using Backbone. I have two questions to ask based on the code below.

第一个问题是在我填写表单并点击按钮后,模型对象应该创建一些默认属性。但是,console.log将使用我在将模型传递给新视图对象之前填写的表单中最新的属性来打印模型。

The first issue is after I fill out the form and click the button, the model object should be created with some default attributes. However, the console.log prints the model with the newest attribute from the form I fill out before I pass the model to the new view object.

第二个问题是我可以成功地将数据保存到db,但是我的成功回调函数没有被调用。有人可以帮我回答这些问题吗?

The second issue is I can successfully save the data to db, but my success call back function is not being called. could someone help me to answer these questions??

var form = document.forms[0];

var RetailerModel = Backbone.Model.extend({
    urlRoot: ' retailer.php',
    defaults: {
        name: 'company-name',
        address: 'company-address',
        phone: 'company-phone',
        icon: 'http://localhost/icon.png'
    }
});

var RetailerCollection = Backbone.Collection.extend({

});

var RetailerView = Backbone.View.extend({

    className: 'retailer',

    template: _.template($('#retailer-template').html()),

    initialize: function() {
        //this.listenTo(this.model, 'change', this.render);

        var obj = {
            name: form.name.value,
            address: form.address.value,
            phone: form.phone.value
        };

        this.model.set(obj);
        //why the successful callback does not work????
        this.model.save(null, {success: function(model, response){console.log('successful');}});
    },

    render: function() {
        $('#retailer-list').append(this.$el.html(this.template(this.model.toJSON())));

        return this;
    }
});

var RetailerViews = Backbone.View.extend({

});

$('#submit').click(function(e){
    var retailer_model = new RetailerModel();
    console.log(retailer_model); // this prints out the new changed attributes instead of the default ones, why???
    var retailer_view = new RetailerView({model: retailer_model});
    form.reset();
});


推荐答案

1获得模型的正确状态



console.log(retailer_model)不会显示模型属性,它会显示整个模型,但 console.log(retailer_model.attributes)将。还要记住, console.log 并不总是正确,特别是如果你修改对象刚刚你日志之后,这可以导致混乱!

1 Get the correct state of the model

console.log(retailer_model) will not show the models attributes, it'll show the whole model, but console.log(retailer_model.attributes) will. Also bear in mind that console.log isn't always on correct, especially if you modify the object just after you log it this can lead to confusion!

要获取模型的实际当前状态,应该对其进行浅层或深层复制。因此,不要使用 console.log(model),您可以使用下划线克隆 method for a shallow copy:

To get the actual current state of the model you should make a shallow or deep copy of it. So rather than console.log(model) you could use underscores clone method for a shallow copy:

console.log(_(model).clone());

为了帮助我们,我们需要更多地了解您的情况。我要找的第一件事是你的服务器是否返回正确的反馈。 Backbone是基于REST标准的系统。您确定您的伺服器传回正确的回应码吗?如果您使用Chrome,请开启开发人员工具,然后开启网路分页,检查您在发布模型时所得到的回应。为了成功回调被触发,服务器应返回200或201的状态。

To help there we'd really need to know more about your circumstances. The first thing I'd look for is whether your server is returning the right feedback. Backbone is a system based on REST standards. Are you sure your server is returning the right response code? If you use chrome, open your developer tools and then the network tab to inspect what response you're getting when posting your model. In order for the success callback to be fired the server should return a status of 200 or 201.

另一种测试方法是查看错误回调是否正在触发: )。

Another way to test that is to see whether the error callback is firing :).

这篇关于主干模型保存和回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆