0

我确实写了以下代码(*)

当我尝试在我的 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;
});
4

1 回答 1

0

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

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

});
于 2012-05-11T12:12:13.810 回答