backbone.js模型在成功获取后不会更新

我试图在项目中使用backbone.js,并尝试使用fetch方法更新模型时遇到问题。 我的模型如下(单例对象,因为我只想要一个实例)

 define([
'underscore',
'backbone'
], function (_, Backbone) {

var UserModel = Backbone.Model.extend({
    defaults: {
        name: "",
        loggedIn: false,
        user: "null",
        pass: "null"
    },

    prepare: function (options) {
        this.loggedIn = false;
        this.user = options.user;
        this.pass = options.pass;
    },

    url: function () {
        return _SERVER_ + "/WebServices/TTMobileAppService.svc/getUserLogin?user=" + this.user+"&pass="+this.pass;
    },

    parse: function (res) {
        // because of jsonp 
        return res.data;
    }

});

return new UserModel();

});

我的观点如下,当一个成功的获取发生时,记录模型和响应的方法updateLogin被调用。

define(['jquery','underscore','backbone','models / user / UserModel','text!templates / login / loginTemplate.html'],function($,_,Backbone,UserModel,loginTemplate){/ /},footerTemplate){

var LoginView = Backbone.View.extend({
    el: $("#loginArea"),
    events: {
        'mouseup #loginButton': 'expandLogin',
        'click #login': 'loginUser'
    },
    initialize: function () {
        this.options = { user: '', pass: "" };
        var that = this;
        this.model = UserModel;
        this.model.on('change', this.render, this);
        this.render();
    },
    render: function () {
        var data = {
            user: this.model.toJSON(),
            _: _
        };
        var compiledTemplate = _.template(loginTemplate, data);
        this.$el.html(compiledTemplate);
    },
    updateLogin: function(model, response, options) {
        console.log(model);
        console.log(response);
        console.log(options);
    },
    expandLogin: function () {
        var button = this.$el.children("div").first().children('#loginButton')[0];
        var box = this.$el.children("div").first().children('#loginBox')[0];
        $(box).fadeToggle(400);
        $(button).toggleClass('active');
    },
    loginUser: function () {
        var that = this;
        var username = $('#username_field', this.el).val();
        var password = $('#password_field', this.el).val();
        this.options = { user: username, pass: password };
        this.model.prepare(this.options);
        this.model.fetch({
            type: "GET",
            error: function (collection, response, options) {
                alert('error');
                alert(response.responseText);
            },
            success: that.updateLogin,
            complete: function () {
                alert('complete');
            },
            dataType: 'jsonp'
        });

    }

});

return LoginView;

});

目前我的模型没有更新

模型在萤火虫 但响应对象是正确和成功的 jsonp对象返回正常

任何帮助将不胜感激


 parse: function (res) {
    // because of jsonp 
    return res.data;
}

需要删除,但它是从示例中复制的,尽管示例中的查询在对象内有一个对象,与我的配置不同。 现在解决=]

链接地址: http://www.djcxy.com/p/67081.html

上一篇: backbone.js model not updating on fetch after success

下一篇: Override fetch method in backbone