Backbone.model:对象函数(a){return new n(a)}没有方法'has'

时间:2012-05-11 11:49:11

标签: javascript backbone.js requirejs

我写了下面的代码(*)

当我尝试在我的js控制台中运行以下代码(**)时,
我得到以下结果:

"your attributes are: ", Object // json object taken from the server as I was expecting  

Object function (a){return new n(a)} has no method 'has' 

为什么我会遇到有关has no method 'has'的问题?

-

(**)

require.config({
    baseUrl: "/"
});

require(["js/models/task"], function ( Model ) {
    var model = new Model({id: 1});
    model.fetch();
    console.log(model.attributes);
});

(*)

define([], function () {
    var MyModel = Backbone.Model.extend({

        initialize: function ()
        {
            this.bind("change", function () {
                console.log("this model has been changed")
            });

            this.bind("error", function (model, error) {
                console.log(error);
            })
        },

        urlRoot: "/",
        url: function () {
            var base = this.urlRoot || (this.collection && this.collection.url) || "/";
            if (this.isNew()) return base;
            return base + this.id;
        },

        validate: function (attribute) {
            if (typeof attribute === "object") {
                console.log("your attributes are: ", attribute);
            }
        }

    });

    return MyModel;
});

1 个答案:

答案 0 :(得分:0)

fetch是异步的,请尝试以下操作:

require(["js/models/task"], function ( Model ) {
    var model = new Model({id: 1});
    model.fetch({success: function() {
        console.log(model.attributes);
    }});

});
相关问题