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 に答える