gpt4 book ai didi

javascript - 'set' 返回未定义的 Backbone Vanilla 模型

转载 作者:行者123 更新时间:2023-11-29 22:01:04 25 4
gpt4 key购买 nike

app.js

define([
'jquery',
'underscore',
'backbone',
'background',
'views/popup',
'views/playlist',
'models/popup',
], function($, _, Backbone, Background, PopupView, PlaylistView, PopupModel) {
var init = function() {
console.log("app: init")
var bg = chrome.extension.getBackgroundPage();
var popupModel = PopupModel();
};

return {
init: init
};

});

models/popup.js

define([
'jquery',
'backbone',
], function($, Backbone){
console.log("inside popupModel") // this is called
var PopupModel = Backbone.Model.extend({
initialize: function() {
alert("hey") // this is NOT called. Error occurs before this.
}
});
// Return the model for the module
return PopupModel;
});

问题:当我尝试创建一个新的 PopupModel 时,我一直收到 undefined is not a function,这可以追溯到 Backbone 中的以下源代码。

var Model = Backbone.Model = function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId('c');
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
attrs = _.defaults({}, attrs, _.result(this, 'defaults'));
this.set(attrs, options); // ERROR HERE; this.set is undefined for an unknown reason.
//console.log(this) outputs a WINDOW object. I don't know why.

我很困惑为什么我会在上述设置中看到这样的错误。谁能帮帮我?

最佳答案

要创建模型的实例,您应该使用 new 关键字。

var popupModel = new PopupModel();

建议不要在模块中不必要的地方定义用于存储模型的变量,只需返回它:return Backbone.Model.extend({}) ;

关于javascript - 'set' 返回未定义的 Backbone Vanilla 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com