我有一个问题,我认为是非常基本的东西但是:
我只看到了具有集合视图的示例,并且单个视图依赖于正在更新的集合.如果您有多个视图尝试订阅集合事件,例如reset,addOne,addAll等,该怎么办?
我是否错过了关于做/不这样做的一些观点?你有这方面的例子吗?这甚至有意义吗?
任何信息都非常感谢
var Coll = Backbone.Collection.extend({
model: SingleModel,
url: 'service',
initialize: function(){
console.log('collection inited')
}
});
var SingleModel = Backbone.Collection.extend({});
var CollView = Backbone.View.extend({
el: 'ul',
template: Handlebars.compile(someContainerTemplate),
init: function(){
_.bindAll(this, 'render', 'addAll', 'addOne');
this.collection.bind("reset", this.addAll);
this.collection.fetch();
},
render: function(){
$(this.el).append(this.template())
},
addAll: function(){
this.collection.each(this.addOne);
},
addOne: function(model){
var view = new SingleView({ model: model })
}
})
var SingleView = Backbone.View.extend({
tagName: "li",
events: {
"click .delete": "remove"
},
template: Handlebars.compile(someTemplateForSingleItem),
initialize: function() {
_.bindAll(this,'render');
this.model.bind('save', this.addOne); …Run Code Online (Sandbox Code Playgroud) 所以我知道如何获得单个虚拟属性,如Mongoose文档中所述:
PersonSchema
.virtual('name.full')
.get(function () {
return this.name.first + ' ' + this.name.last;
});
Run Code Online (Sandbox Code Playgroud)
但是如果我的架构是:
var PersonSchema = new Schema({
name: {
first: String
, last: String
},
arrayAttr: [{
attr1: String,
attr2: String
}]
})
Run Code Online (Sandbox Code Playgroud)
我想为arrayAttr中的每个嵌套对象添加一个虚拟属性:
PersonSchema.virtual('arrayAttr.full').get(function(){
return attr1+'.'+attr2;
});
Run Code Online (Sandbox Code Playgroud)
Lemme知道我在这里错过了什么.
我必须做一些天真的事情,但我无法在图表中渲染我的数据.
这是我的示例数据:
var data = [
{
date: '02-12-2013',
val: 13,
}
{
date: '02-13-2013',
val: 6,
}
...
]
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
var cf = crossfilter(data);
var dim = cf.dimension(function(d){ return d.val });
var group = dim.group();
var barchart = dc.barChart('#container');
barchart.width(850)
.height(250)
.margins({top: 10, right: 50, bottom: 30, left: 40})
.dimension(dim)
.group(group)
.gap(1)
.x(d3.time.scale()
.domain([new Date(data[0].date),
new Date(data[data.length-1].date)]))
.xUnits(d3.time.day)
.centerBar(true)
.renderHorizontalGridLines(true)
.renderVerticalGridLines(true)
.render();
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?这是图表的屏幕截图:

我想包括应用程序实例来使用它的事件聚合器显示在这里
当我在视图中包含实例时出现错误.
从App.Bootloader.js中的Requirejs配置文件中解决问题:
require(['App'], function (App){
App.start();
});
Run Code Online (Sandbox Code Playgroud)
来自App.js:
define(function (require){
//...requisite includes $, _, Backbone, Marionette ...
var Layout = require('Layout');
var App = new Marionette.Application();
App.addRegions({
main: '#view_content'
});
App.addInitializer(function (){
App.main.show(new Layout());
//... adding router etc ...
Backbone.Marionette.TemplateCache.loadTemplate = function (template, callback){
callback.call(this, Handlebars.compile(template));
};
Backbone.history.start();
});
return App;
});
Run Code Online (Sandbox Code Playgroud)
来自Layout.js:
define(function(require){
var View = require('folder/folder/View');
//template contains #sub div
var template = require('text!template.html');
return Marionette.Layout.extend({
template: template,
regions: {
sub: '#sub'
},
initialize: function(){
//wait …Run Code Online (Sandbox Code Playgroud) javascript ×3
backbone.js ×2
crossfilter ×1
d3.js ×1
dc.js ×1
express ×1
js-amd ×1
marionette ×1
mongodb ×1
mongoose ×1
node.js ×1
requirejs ×1