在Backbone中,我正在使用新Backbone.listenTo事件.我的一个实例让听众附加了三个不同的事件,例如:
this.listenTo(this._Collection, 'reset add change', this._setCollection);
Run Code Online (Sandbox Code Playgroud)
当它被认为是适当的时候被称为并且那里没有问题.我不知道的是如何找出触发了哪个事件.我可以使用以下方法访问e参数:
_setCollection: function(e) {
// Do fun stuff
}
Run Code Online (Sandbox Code Playgroud)
问题是e参数只发送集合的副本,而没有提到实际触发了什么事件.我试过了e.type,e.target但那些物品不存在.以下是eChrome Dev工具中对象的副本:
_byCid: Object
_byId: Object
_events: Object
add: Array[1]
change: Array[1]
remove: Array[1]
reset: Array[1]
__proto__: Object
_listenerId: "l16"
length: 3
models: Array[3]
Run Code Online (Sandbox Code Playgroud)
如何找到触发的事件?
编辑:答案澄清:尽管标记的答案在技术上是正确的,正如mu_is_too_short所指出的,正确答案是使用多个处理程序而不执行此类"chicanery"
javascript backbone.js backbone-events backbone.js-collections
我试图扩展骨干集合,第一种方式我在声明中做了新的事情,在第二种方式我首先声明然后我创建了一个新的实例.做第一个错,有什么区别?
var AppointmentList = new Backbone.Collection.extend({
model: Appointment
});
Run Code Online (Sandbox Code Playgroud)
和
var AppointmentList = Backbone.Collection.extend({
model: Appointment
});
var aptlist = new AppointmentList();
Run Code Online (Sandbox Code Playgroud) 每当我将新模型添加到我的集合中时,我都会尝试更新我的视图.我的第一个问题是,当我保存该模型时,我会自动将模型添加到我的集合中,例如:
PostsApp.Views.Form = Backbone.View.extend({
template: _.template($('#form-template').html()),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
},
events:{
'click button' : 'save'
},
save: function(e){
console.log("is this working");
e.preventDefault();
var newname = this.$('input[name=name-input]').val();
var newadress = this.$('input[name=adress-input]').val();
this.model.save({name: newname, adress : newadress});
}
});
Run Code Online (Sandbox Code Playgroud)
还是我还要做collection.add()
除了在我的视图中看到新模型,我试图添加这样的'添加'事件监听器:
PostsApp.Views.Posts = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
},
render: function(){
this.collection.forEach(this.addOne, this);
},
addOne: function(post){
var postView = new PostsApp.Views.Post({model:post});
postView.render();
this.$el.append(postView.el);
}
});
Run Code Online (Sandbox Code Playgroud)
这不仅不起作用,而且当我添加初始化方法时,它只是在首次加载页面时复制模型中的所有内容.
javascript jquery backbone.js backbone-events backbone.js-collections
简单的问题:什么是"只有一次"发起事件的最佳方式?
我有一个骨干系列,有多个模型.有时,一次更新多个模型的属性,在集合上触发多个"更改"事件.
只触发一次"更改"事件的最佳方法是什么,将所有属性更改分组在一起?
我最好的想法是使用计时器,但这只会捕获第一个模型属性更改.
所有建议,想法和解决方案都受到重视.谢谢 :).
上下文:在这种情况下,事件会触发计算密集型函数,因此必须避免多次运行.
我想传递并从视图中获取一个值来模拟集合的使用,我能够将值传递给模型,当我使用集合时它不起作用.我不知道这里的问题在哪里就是我的代码.
我的模特
var PostwallModel=Backbone.Model.extend({
urlRoot: 'http://localhost:3400/post',
idAttribute: '_id',
defaults : {
userId: '',
userName: " ",
postmsg : "unknown"
},
initialize: function() {
console.log("<><><>post model initialize<><><><><>");
},
// Delete item (row) from
clear: function() {
this.destroy();
}
});
Run Code Online (Sandbox Code Playgroud)
我的收藏
var PostwallCollection = Backbone.Collection.extend({
url: 'http://localhost:3400/post',
model: PostwallModel
});
**here is my view**
var PostwallView = Backbone.View.extend({
el: $("#page"),
template: _.template(PostwallTemplate),
events: {
'click #postinwall' : 'submitpost',
},
initialize: function() {
console.log("_______postmodel");
this.model = new PostwallModel();
var obj= new PostwallModel();
obj.set({userId:'123',userName:"str …Run Code Online (Sandbox Code Playgroud) javascript jquery backbone.js backbone.js-collections cordova-3
以下示例代码运行良好:
Auth_controller.prototype.isLogged = function(){
//Check if the user is authenticated
var getAuthStatus = this.auth_model.fetch();
return getAuthStatus;
};
Auth_controller.prototype.redirect = function(fragment, args, next){
var getAuthStatus = this.isLogged();
var self = this;
$.when(getAuthStatus).then(function(response){
//Do something with the response
}
});
Run Code Online (Sandbox Code Playgroud)
这似乎不适用于Collection.
当我控制台登录集合时,我得到一个空集合.
我知道我可以在方法中使用成功回调函数(已经测试过),但我不想这样做,因为我希望函数返回一个我可以从其他函数调用的promise.
编辑 - >不,抱歉它在成功回调中也不起作用.
有关解决方法的任何建议吗?
编辑;
此图显示了从模型和集合获取方法返回的内容.
除非我做错了很明显,否则我不明白为什么会这样.
当控制台在成功回调中记录返回的响应时,我看到屏幕截图中显示的空对象已填充.

EDIT2:
这就是我的收藏品:
define([
/*--- libraries ---*/
'jquery',
'underscore',
'backbone',
/*--- model ---*/
'models/users/role_model'
], function($, _, Backbone,
Role_model){
var Role_collection = Backbone.Collection.extend({
url: '/ingeb/api_v1/users/roles',
model: Role_model
});
return Role_collection;
});
Run Code Online (Sandbox Code Playgroud) javascript jquery promise backbone.js backbone.js-collections
我正在骨干中构建一个简单的待办事项.我的HTML是:
<!DOCTYPE html>
<html>
<head>
<title>back bone</title>
<script src="js/lib/jquery.js"></script>
<script src="js/lib/underscore.js"></script>
<script src="js/lib/backbone.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<h2>Total Things Todo: <div id="no-todo"> 0 </div></h2>
<ul id="todo-list" class="unstyled">
</ul>
<div>
<button class="btn" id="add-todo"><i class="icon-plus"></i>Add Todo</button>
</div>
<script src="js/script.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的骨干代码看起来像这样:
$(document).ready(function(){
var Todo = Backbone.Model.extend({
id: 0,
text:null,
completed: false
});
var Todos = Backbone.Collection.extend({
model:Todo,
initialize: function(models, options) {
this.on("add",options.view.addTodoLi);
}
});
var AppView = Backbone.View.extend({
el: $("body"),
initialize: function() {
this.todos …Run Code Online (Sandbox Code Playgroud) 我有必须调用4的外部API如集合:http://www.abc.com,http://www.fgt.com,http://www.jkl.com和HTTP://www.rty .com.
我有一个名为Todos.js的Collection.有没有办法可以在一个集合中一起获取4个api,因为所有四个api都会为我提供相同的模型响应所以我从4 apis得到的响应具有相同的数据结构,即"name"和"link".
有没有办法可以在同一个集合中附加所有回复?实现这一目标的最佳方法是什么?
javascript jquery backbone.js backbone.js-collections backbone-model
我是Backbone JS的新手,一直关注Christopher Coenraets Wine Cellar教程.
这一切都很好,花花公子,但我不明白他是如何使用this.model.models访问集合而不是this.collection.此外,当我尝试将代码更改为后者时,似乎this.collection未定义.
window.WineListView = Backbone.View.extend({
tagName:'ul',
initialize:function () {
this.model.bind("reset", this.render, this);
},
render:function (eventName) {
_.each(this.model.models, function (wine) {
$(this.el).append(new WineListItemView({model:wine}).render().el);
}, this);
return this;
}
});
Run Code Online (Sandbox Code Playgroud) 当我使用该Backbone.Collection.where函数来过滤集合时,我得到一个模型数组作为返回值,但不是另一个过滤的集合对象.所以我不能使用其他收集功能.
这种行为的目的是什么?