【问题标题】:backbone.js model not updating on fetch after success成功后,backbone.js 模型未在获取时更新
【发布时间】:2014-03-21 03:01:45
【问题描述】:

我正在尝试在项目中使用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被调用,模型和响应被记录。

定义([ 'jquery', '下划线', '骨干', '模型/用户/用户模型', '文本!模板/登录/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;

});

目前我的模型没有更新

但响应对象正确且成功

任何帮助将不胜感激

【问题讨论】:

    标签: javascript backbone.js underscore.js


    【解决方案1】:
     parse: function (res) {
        // because of jsonp 
        return res.data;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 2012-03-30
      • 1970-01-01
      • 2012-02-10
      相关资源
      最近更新 更多