扩展Backbone.Collection/View/Models的最佳方法

Lor*_*ard 1 javascript backbone.js

我需要使用一组函数扩展backbone.collection:

例:

var collection1 = Backbone.Collection.extend({});
var collection2 = Backbone.Collection.extend({});
Run Code Online (Sandbox Code Playgroud)

应该有以下自定义方法:

console.log(collection1.method1); // function () {}
console.log(collection1.method2); // function () {}
Run Code Online (Sandbox Code Playgroud)

另一方面

var collection3 = Backbone.Collection.extend({});
Run Code Online (Sandbox Code Playgroud)

不应该有这些方法.

console.log(collection3.method1); // undefined
Run Code Online (Sandbox Code Playgroud)

如何为collection1和collection2扩展Backbone.Collection而不为collection3扩展?

ant*_*rna 10

你的意思是这样的:

var CustomCollection = Backbone.Collection.extend({
    method1: function() {},
    method2: function() {}
});
var collection1 = CustomCollection.extend({});
var collection2 = CustomCollection.extend({});

var collection3 = Backbone.Collection.extend({});
Run Code Online (Sandbox Code Playgroud)