骨干模型得到未定义的错误

时间:2014-03-13 13:07:36

标签: javascript jquery backbone.js

app.Model.Brand = Backbone.Model.extend({});
app.Collection.BrandCollection = Backbone.Collection.extend({
    model: app.Model.Brand,
    parse: function (response) {
        return response.brandDTOList;
    }
});
var brandcollection = new app.Collection.BrandCollection();
brandcollection.url = '/brands';
brandcollection.fetch({
    success: function (collection, response, options) {
        app.views.brandline = new app.View.BrandPanelView({
            model: brandcollection
        });
        $('#tab-content').empty();
        $('#tab-content').append(app.views.brandline.render());
    }
});

在此视图中,传递单个模型。

app.View.BrandItemPanelView = Backbone.View.extend({
    initialize: function (options) {
        this.views = {};
        this.model.bind('destroy', this.remove, this);
    },
    events: {
        'click .bra-edt': 'brandEditAction',
    },
    brandEditAction: function () {
        this.model.get('image');
    }
});

当我this.model.get('image');时,我get不是函数。 我不知道为什么会出现这样的错误。

我尝试深度克隆它但仍然没有成功。价值观就在那里。

var newModel = new app.Model.Brand();
newModel = $.extend(true, {}, self.model);
newModel.get('image')

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您如何实例化BrandItemPanelView?

您必须明确传递模型:

var view = new app.View.BrandItemPanelView({ model: myModel });

如果myModel实际上是一个Backbone模型,它应该有get函数......

 view.model // should return your model
 view.brandEditAction() // should run the function and get the 'image' attribute from your model

答案 1 :(得分:0)

您的问题是传递 BrandCollection 的问题,而不是将其作为collection传递给model

所以而不是

app.views.brandline = new app.View.BrandPanelView({
            model: brandcollection
});

您可能想要

app.views.brandline = new app.View.BrandPanelView({
    collection: brandcollection
});