したがって、私のアプリでは、jquery $.get メソッドを介して一連のデータを取得しており、そのデータを使用してコレクションをリセットしようとしています。
var self = this;
$.get('/api/project/active', function(data) {
self.dropdownListProjectCollection.reset(data);
});
dropdownListProjectCollection
次のように初期化関数に設定されます。
initialize: function() {
$(window).on('resize', this.responsiveMenu);
this.dropdownListProjectCollection = new app.ProjectCollection;
console.log(this.dropdownListProjectCollection);
this.dropdownListProjectCollection.on('reset', this.populateMenu, this);
this.render();
return this;
},
console.log
は空のコレクションを返します。
私は何を得Uncaught TypeError: undefined is not a function
ますか?エラーは、
self.dropdownListProjectCollection.reset(data)
ライン。
どうしたの?それは私のアプリの残りの部分で完全に機能します。
getActiveProjects メソッド内の自身の console.log
r {cid: "view1", options: Object, $el: e.fn.e.init[1], el: div.contentwrap, dropdownListProjectCollection: ProjectCollection…}
$el: e.fn.e.init[1]
cid: "view1"
dropdownListProjectCollection: ProjectCollection
el: div.contentwrap
options: Object
__proto__: s
およびコールバック内の self の console.log、
r {cid: "view1", options: Object, $el: e.fn.e.init[1], el: div.contentwrap, dropdownListProjectCollection: ProjectCollection…}
$el: e.fn.e.init[1]
cid: "view1"
dropdownListProjectCollection: ProjectCollection
el: div.contentwrap
options: Object
__proto__: s
リセット前の dropdownListProjectCollection
ProjectCollection {length: 0, models: Array[0], _byId: Object, _events: Object, constructor: function…}
_byId: Object
_events: Object
length: 0
models: Array[0]
__proto__: ctor
プロジェクトコレクション
ProjectCollection = (function(_super) {
__extends(ProjectCollection, _super);
function ProjectCollection() {
return ProjectCollection.__super__.constructor.apply(this, arguments);
}
ProjectCollection.prototype.url = "/api/project/projects";
ProjectCollection.prototype.model = app.Project;
ProjectCollection.prototype.archived = function() {
return new ProjectCollection(this.where({
status: '3'
}));
};
ProjectCollection.prototype.active = function() {
return new ProjectCollection(this.where({
status: '1'
}));
};
ProjectCollection.prototype.pending = function() {
return new ProjectCollection(this.where({
status: '7'
}))
};
ProjectCollection.prototype.completed = function() {
return new ProjectCollection(this.where({
status: '5'
}));
}
ProjectCollection.prototype.comparator = function(model) {
return -model.get("creation_date_unix");
};
ProjectCollection.prototype.search = function(searchTerm, filters) {
var pattern, status = [];
pattern = new RegExp(searchTerm, "gi");
// Loop throught the filters and push there numeric value to an array.
for (var k in filters) {
if(k == "pending" && filters["pending"] == true) {
var pending = this.pending();
}
if(k == "active" && filters["active"] == true) {
var active = this.active();
}
if(k == "completed" && filters["completed"] == true) {
var completed = this.completed();
}
if(k == "archived" && filters["archived"] == true) {
var archived = this.archived();
}
}
var filteredCollection = new ProjectCollection;
if(pending !== undefined) {
filteredCollection.add(pending.models);
}
if(active !== undefined) {
filteredCollection.add(active.models);
}
if(completed !== undefined) {
filteredCollection.add(completed.models);
}
if(archived !== undefined) {
filteredCollection.add(archived.models);
}
if(searchTerm != "") {
return _(filteredCollection.filter(function(project){
return pattern.test(project.get("project_name") + project.get("client_name"));
}));
}
return filteredCollection;
/*// Filter the collection based on the status attribute of
// the model. If the value of a key is undefined (not met the criteria in the loop)
// the value will be undefined and return empty (false).
var filteredCollection = this.filter(function(project){
return project.get("status") == status[0] ||
project.get("status") == status[1] ||
project.get("status") == status[2] ||
project.get("status") == status[3] ;
});
console.log(filteredCollection);
*/
};
return ProjectCollection;
})(app.BaseCollection);