Backbone.js 主干、requirejs、多视图、一个集合
下面您可以看到四个基本的requireJS文件。如何让多个Backbone.js视图共享一个已在别处启动和获取的集合 请注意 我知道我可以在App.js中传递集合,但我不想这样做,因为我可能有许多集合需要在许多视图中使用,我不想在App.js中传递每个集合 Collection.jsBackbone.js 主干、requirejs、多视图、一个集合,backbone.js,requirejs,Backbone.js,Requirejs,下面您可以看到四个基本的requireJS文件。如何让多个Backbone.js视图共享一个已在别处启动和获取的集合 请注意 我知道我可以在App.js中传递集合,但我不想这样做,因为我可能有许多集合需要在许多视图中使用,我不想在App.js中传递每个集合 Collection.js return Backbone.Collection.extend({ url: '...' }); var collection = new Collection(); $.when(collecti
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
App.js
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
View1.js
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
View2.js
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
快速回答:RequireJS只运行一次函数体代码,多次单独运行
return
语句。因此,您可以创建一个集合实例,并将其返回给每个需要它的人
Collection.js
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
或者,如果您想要运行多个CollectionInstance实例,并且不想将其传递给视图,那么我不认为缺少全局变量会有帮助。这看起来像: Globals.js
return Backbone.Collection.extend({
url: '...'
});
var collection = new Collection();
$.when(collection.fetch()).done(function(){
new View1();
new View2();
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
define(['Collection'], function(Collection){
return Backbone.View.extend({
initialize: function(){
console.log('Need the initiated, fetched collection here...');
});
});
});
var CollectionConstructor = Backbone.Collection.extend({
url: '...'
});
var CollectionInstance = new CollectionConstructor();
return CollectionInstance;
var AppGlobals = AppGlobals || {};
AppGlobals.oneSet = new Collection ();
AppGlobals.anotherSet = new Collection ();
您现在可以让您的视图依赖于Globals.js并从这里访问它们。根据您的使用情况,这两种方法中的任何一种都应该有效。请记住,在第二种方法中,您的Collection.js是未修改的