Javascript 主干+;RequireJS:让收藏持久化?
我在RequireJS中使用主干,我的视图需要在20个集合之间切换,这些集合对应于各自的RESTful API “正常”的方式处理事情很好,除了需要为每个API定义一个新集合,从而导致代码库极度膨胀: 收集(x20次) 查看Javascript 主干+;RequireJS:让收藏持久化?,javascript,backbone.js,requirejs,Javascript,Backbone.js,Requirejs,我在RequireJS中使用主干,我的视图需要在20个集合之间切换,这些集合对应于各自的RESTful API “正常”的方式处理事情很好,除了需要为每个API定义一个新集合,从而导致代码库极度膨胀: 收集(x20次) 查看 define(function(require) { var Backbone = require('backbone'), c1 = require('./collections/collection-1'), ...
define(function(require) {
var Backbone = require('backbone'),
c1 = require('./collections/collection-1'),
...
c20 = require('./collections/collection-20');
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
define(['Backbone', 'collections'], function(Backbone, collections) {
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
只做返回c在集合内部调用newc({url:/path/to/api-1})代码>在视图中,我能够减少高度重复的集合定义;但是现在在每个新的c()代码>调用时,API总是会被戳来检索数据的新副本,这是对资源的浪费,也是我不想要的
有没有办法使集合“持久化”?哪个更好地满足以下所有标准:
- 对于每个API的对应集合,只执行一个
new
李>
- 每个集合可以在使用RequireJS定义的不同视图之间共享/访问李>
- 集合的定义可以被所有API重用李>
- 全局名称空间根本没有被污染
您不需要创建新的文件/集合
在你看来,你可以这样做:
var collections = [];
collections.push( Backbone.Collection.extend({url: '/path/to/api-1'});
collections.push( Backbone.Collection.extend({url: '/path/to/api-2'});
collections.push( Backbone.Collection.extend({url: '/path/to/api-3'});
然后,您将有一个包含所有集合的数组您不需要创建新文件/集合
在你看来,你可以这样做:
var collections = [];
collections.push( Backbone.Collection.extend({url: '/path/to/api-1'});
collections.push( Backbone.Collection.extend({url: '/path/to/api-2'});
collections.push( Backbone.Collection.extend({url: '/path/to/api-3'});
然后,您将有一个包含所有集合的数组与另一个答案类似,您可以将集合数组定义为AMD模块:
收藏
define(function(require) {
var Backbone = require('backbone');
var c = Backbone.Collection.extend({
url: '/path/to/api-1'
});
return new c();
});
define(['backbone', 'underscore'], function(Backbone, _) {
var collections = [];
var urls = ['/path/to/api-1', '/path/to/api-2', '/path/to/api-3'];
_(urls).each(function(url) {
collections.push(Backbone.Collection.extend({
url: url
}));
});
return collections;
});
您的视图可以这样使用它:
查看
define(function(require) {
var Backbone = require('backbone'),
c1 = require('./collections/collection-1'),
...
c20 = require('./collections/collection-20');
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
define(['Backbone', 'collections'], function(Backbone, collections) {
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
与另一个答案类似,您可以将集合数组定义为AMD模块:
收藏
define(function(require) {
var Backbone = require('backbone');
var c = Backbone.Collection.extend({
url: '/path/to/api-1'
});
return new c();
});
define(['backbone', 'underscore'], function(Backbone, _) {
var collections = [];
var urls = ['/path/to/api-1', '/path/to/api-2', '/path/to/api-3'];
_(urls).each(function(url) {
collections.push(Backbone.Collection.extend({
url: url
}));
});
return collections;
});
您的视图可以这样使用它:
查看
define(function(require) {
var Backbone = require('backbone'),
c1 = require('./collections/collection-1'),
...
c20 = require('./collections/collection-20');
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
define(['Backbone', 'collections'], function(Backbone, collections) {
var v = Backbone.View.extend({
/* Some methods to toggle between Collections here */
});
return v;
});
找到了一个相当老套的解决方案:
用另一个型号包装您的系列:
define(function(require) {
var Backbone = require('backbone'),
m = require('../models/model');
var wrapper = Backbone.Model.extend({
initialize: function() {
var $_path = this.get('path');
var c = Backbone.Collection.extend({
model: m,
url: $_path,
initialize: function() {
this.deferred = this.fetch();
}
});
this.set({
collection: new c()
});
}
});
return wrapper;
});
然后,定义缓存容器:
define(function(require) {
return cache = {};
});
在视图模块中需要缓存容器和包装的集合:
define(function(require) {
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
cache = require(cache),
wrapper = require('../collections/collection');
var v = Backbone.View.extend({
render: function($_path) {
var c = cache[$_path] || cache[$_path] = new wrapper({
path: $_path
}).get('collection'));
}
....
});
return v;
});
通过这种方式,您既可以使用动态生成的$\u path
值实例化新集合,又可以在页面未刷新的情况下缓存从API获取的数据—如果您希望数据在页面刷新过程中保持不变,请实现localStorage
。找到了一个相当粗糙的解决方案:
用另一个型号包装您的系列:
define(function(require) {
var Backbone = require('backbone'),
m = require('../models/model');
var wrapper = Backbone.Model.extend({
initialize: function() {
var $_path = this.get('path');
var c = Backbone.Collection.extend({
model: m,
url: $_path,
initialize: function() {
this.deferred = this.fetch();
}
});
this.set({
collection: new c()
});
}
});
return wrapper;
});
然后,定义缓存容器:
define(function(require) {
return cache = {};
});
在视图模块中需要缓存容器和包装的集合:
define(function(require) {
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
cache = require(cache),
wrapper = require('../collections/collection');
var v = Backbone.View.extend({
render: function($_path) {
var c = cache[$_path] || cache[$_path] = new wrapper({
path: $_path
}).get('collection'));
}
....
});
return v;
});
通过这种方式,您既可以使用动态生成的$\u path
值实例化新集合,又可以在页面未刷新的情况下缓存从API获取的数据—如果希望数据在页面刷新过程中保持不变,请实现localStorage
。,集合
不能由AMD格式定义的不同视图模块共享/访问,除非它被设置为全局对象-这是我想避免的另一件事。它们可以实例化并传递到所有需要它们作为参数的视图中。但是,集合
不能由AMD格式定义的不同视图模块共享/访问,除非它是一个全局对象-这是我想避免的另一件事。它们可以实例化并传递到所有需要它们作为参数的视图中。问题是我想保留在实例化时定义url
的灵活性,所以这可能不起作用。问题是我想保留在实例化时定义url
的灵活性,所以这可能不起作用。