我是Backbone的新手,并且一直在调整骨架底漆以用于我的休息api。一切正常,除了我无法找出将新添加的项目附加到列表视图的最佳方法。这是我的HTML:
<div id="subscriberApp" class="row">
<div class="col-md-6">
<h2>Subscribers List</h2>
<table class="table">
<thead>
<tr>
<th>Login</th>
<th>Uri</th>
<th>Action</th>
</tr>
</thead>
<tbody class="subscribers-list"></tbody>
</table>
</div>
<div class="col-md-3">
<div>
<h2>Add a Subscriber</h2>
<p><label for="id_login">Login:</label> <input class="form-control" id="id_login" maxlength="100" name="login" style="display:block" type="text" /></p>
<p><label for="id_password">Password:</label> <input class="form-control" id="id_password" maxlength="100" name="password" style="display:block" type="text" /></p>
<p><label for="id_realm">Realm:</label> <input class="form-control" id="id_realm" maxlength="100" name="realm" style="display:block" type="text" /></p>
<p><label for="id_ip_address">Ip address:</label> <input class="form-control" id="id_ip_address" maxlength="100" name="ip_address" style="display:block" type="text" /></p>
<button class="btn btn-success create">Create</button>
</div>
</div>
</div>
<script type="text/template" id="subscribers-tmpl">
<td><span class="login"><%= login %></span></td>
<td><span class="uri"></span></td>
<td><button class="btn btn-warning edit-subscriber">Edit</button> <button class="btn btn-danger remove">Delete</button></td>
</script>
<script src="/static/subscribers/underscore.js"></script>
<script src="/static/subscribers/backbone.js"></script>
<script src="/static/subscribers/script.js"></script>
这是我的主干脚本:
var subscribers_model = Backbone.Model.extend({
defaults: {
id: null,
login: null,
password: null,
realm: null,
hotspot_ip: null,
}
});
var subscribers_collection = Backbone.Collection.extend({
url: 'http://example.net/subscribers',
model: subscribers_model,
parse: function(data) {
return data;
}
});
var SubscriberView = Backbone.View.extend({
tagName: 'tr',
template: _.template($('#subscribers-tmpl').html()),
initialize: function() {
this.listenTo(this.model, 'destroy', this.remove)
},
render: function() {
var html = this.template(this.model.toJSON());
this.$el.html(html);
return this;
},
events: {
'click .remove': 'onRemove'
},
onRemove: function() {
this.model.destroy();
}
});
var SubscribersView = Backbone.View.extend({
el: '#subscriberApp',
initialize: function() {
this.listenTo(this.collection, 'sync', this.render);
},
render: function() {
var $list = this.$('.subscribers-list').empty();
this.collection.each(function(model) {
var item = new SubscriberView({model:model});
var uri = item.model.attributes.uri
item.model.attributes.id = uri.replace('/subscribers/', '');
$list.append(item.render().el);
}, this);
return this;
},
events: {
'click .create': 'onCreate'
},
onCreate: function() {
var self = this;
var $login = this.$('#id_login');
var $password = this.$('#id_password');
var $realm = this.$('#id_realm');
var $ip = this.$('#id_ip_address');
this.collection.create({
login: $login.val(),
password: $password.val(),
realm: $realm.val(),
hotspot_ip: $ip.val()
});
login: $login.val('');
password: $password.val('');
realm: $realm.val('');
hotspot_ip: $ip.val('');
}
});
var subscribers = new subscribers_collection();
var SubsView = new SubscribersView({collection: subscribers});
subscribers.fetch();
所以我认为主要的问题是,当我获取所有资源时,我会在响应中获得一个资源ID foreach。我用这个资源ID来删除单个资源。这工作正常。 但是,当我创建一个新资源时,我得到的全部是服务器成功200响应,因此创建了资源,但我不能将另一行追加到我的listview。 如果有人可以提出解决方案或替代方法,那么那就是救星。
干杯
答案 0 :(得分:1)
编辑集合视图以在添加到集合时显示该项目。
追加初始化:
this.listenTo(this.collection, 'add', this.on_item_add);
添加功能
on_item_add:function(added_item){
var added_item_row = new SubscriberView({model: added_item});
$(".subscribers-list").append(added_item_row.render().el);
},