这是我对集合的看法
var mssg = mssg || {};
mssg.MessagesView = Backbone.View.extend({
el: '#messages',
initialize: function() {
this.collection.fetch();
this.collection.bind('reset', this.render, this);
},
render : function() {
this.$el.html('');
this.collection.each(function( item ) {
this.renderMessage( item );
}, this );
return this;
},
renderMessage : function( item ) {
var messageView = new mssg.MessageView({
model : item
});
this.$el.append( messageView.render().el );
}
});
Run Code Online (Sandbox Code Playgroud)
这是集合
var mssg = mssg || {};
mssg.Messages = Backbone.Collection.extend({
model : mssg.Message,
url : 'messages'
});
Run Code Online (Sandbox Code Playgroud)
这就是它的初始化方式:
var mssg = mssg …
Run Code Online (Sandbox Code Playgroud) 假设我有以下代码(完全没用,我知道)
function add( a, b, c, d ) {
alert(a+b+c+d);
}
function proxy() {
add.apply(window, arguments);
}
proxy(1,2,3,4);
Run Code Online (Sandbox Code Playgroud)
基本上,我们知道apply需要一个数组作为第二个参数,但我们也知道这arguments
不是一个合适的数组.代码按预期工作,所以可以安全地说我可以传递任何类似数组的对象作为第二个参数apply()
吗?
以下内容也适用(至少在Chrome中):
function proxy() {
add.apply(window, {
0: arguments[0],
1: arguments[1],
2: arguments[2],
3: arguments[3],
length: 4
});
}
Run Code Online (Sandbox Code Playgroud)
更新:似乎我的第二个代码块在IE <9中失败而第一个(传递arguments
)工作.错误是Array or arguments object expected
,因此我们将得出结论,传递总是安全的arguments
,而在oldIE中传递类似数组的对象是不安全的.
我有一个实现的Angular组件ControlValueAccessor
,但从writeValue
不使用初始值调用该方法ngModel
.
模板:
<my-comp [(ngModel)]="$ctrl.activeUser"></my-comp>
该组件通过以下方式降级为AngularJS:
.directive('myComp', downgradeComponent({
component: MyComp,
inputs: [],
outputs: [],
}));
Run Code Online (Sandbox Code Playgroud)
我尝试添加ngModel
至inputs
和outputs
,但它不工作.
我正在从一个看起来像这样的数组中播种我的数据库(单词和定义有多对多关系):
var seeds = [
{
"word": "Click",
"definitions": ["Computer", "Mouse", "Tasto", "Pulsante", "Selezionare"]
}, {
"word": "Galoppo",
"definitions": ["Cavallo", "Andatura", "Trotto", "Ippica", "Passo"]
}, {
"word": "Raggio",
"definitions": ["Sole", "Bicicletta", "Diametro", "Luce", "Laser"]
}, {
.
.
.goes on for 1089 objects
Run Code Online (Sandbox Code Playgroud)
这是我试过的:
exports.seed = function (knex, Promise) {
var promises = seeds.map(function (seed) {
return knex('words').insert({
word: seed.word
}, 'id').then(function (word_id) {
var promises = seed.definitions.map(function (definition) {
return knex('definitions').insert({
definition: definition
}, 'id').catch(function (err) {
if (err.code …
Run Code Online (Sandbox Code Playgroud)