[英]Derby.js - adding/removing unique list elements
I have this as my bootstrapped data, which I push to the model when nothing is retrieved from the DB: 我将其作为自举数据,当从数据库中检索不到任何内容时,将其推送至模型:
var liveaudience = {
triggers : [
{
'trigger_id': 'vocal_stomp',
'duration': 1000,
'color': '#F23000',
'sound': 'A#'
},
{
'trigger_id': 'guitar_stomp',
'duration': 600,
'color': '#CC0234',
'sound': 'Cmaj'
},
{
'trigger_id': 'drum_pad',
'duration': 1200,
'color': '#CF2451',
'sound': 'Emin'
}
]
};
I have this one route, which does this and then renders these three bootstrapped elements on the template: 我有这条路线,可以这样做,然后在模板上呈现这三个自举元素:
get('/:triggerId?', function(page, model, params){
var triggers = model.get('liveaudience.triggers');
if(typeof triggers === 'undefined'){
// bootstrap triggers
model.push('liveaudience.triggers', liveaudience.triggers);
}
model.subscribe("liveaudience.triggers", function(err, scopedModels){
page.render({'triggers': scopedModels.get()[0]});
});
});
and then here is the template: 然后是模板:
<ul id="triggers">
{{#each triggers}}
<li data-id="{{id}}" class="trigger" style="background-color:{{color}};">
<span class="trigger-label">{{trigger_id}}</span>
<a x-bind="click:removeTrigger" class="remove-trigger">x</a>
</li>
{{/}}
</ul>
Everything shows up, but {{id}} returns a function, and I'd like it to be a uniquely assigned GUID . 一切都显示了,但是{{id}}返回了一个函数,我希望它是一个唯一分配的GUID 。 The main issue is that I don't know how to get & remove this element from the DOM when clicking and firing the removeTrigger
handler. 主要问题是,在单击并触发removeTrigger
处理程序时,我不知道如何从DOM中获取和删除此元素。 Looking at examples, I've seen this within the handler: 查看示例,我已经在处理程序中看到了这一点:
model.at(e.target).remove();
but that doesn't work. 但这不起作用。
Any ideas?! 有任何想法吗?!
Instead of passing triggers into the page.render try using a model.ref like so 与其将触发器传递到page.render中,不如尝试使用model.ref
For Derby 0.5 对于德比0.5
var triggers = model.at('liveaudience.triggers');
model.subscribe(triggers, function(err){
model.ref('_page.triggers', triggers);
page.render();
});
For Derby 0.3 对于Derby 0.3
model.subscribe('liveaudience.triggers', function(err,scopedModels){
model.ref('_page.triggers', scopedModels);
page.render();
});
Then accessing the triggers in the loop like so 然后像这样访问循环中的触发器
{{#each _page.triggers}}
This should setup the bindings correctly. 这应该正确设置绑定。
And then to remove (for Derby 0.5, not absolutely sure for 0.3) 然后删除(对于Derby 0.5,不是绝对确定的0.3)
e.at().remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.