저는 백본과 밑줄에 비교적 익숙하지 않습니다. 그 중 하나는 정말 문제가되지 않습니다. 호기심으로 나를 괴롭 히고 있습니다.백본/밑줄 uniqueId() 이상한 숫자
나는 컬렉션에서 모델을 추가 및 제거하고 브라우저에서 렌더링 할 수있는 아주 간단한 앱을 만들었습니다. 또한 console.log
컬렉션 (그래서 내 컬렉션을 볼 수 있습니다) 능력이 있습니다.
다음은 이상한 점입니다. 생성되는 ID는 1,3,5...
입니다. 내 코드 또는 BB/US와 관련이있는 이유가 있습니까? http://jsfiddle.net/ptagp/
그리고 코드 : 여기
는 작업 바이올린의App = (function(){
var AppModel = Backbone.Model.extend({
defaults: {
id: null,
item: null
}
});
var AppCollection = Backbone.Collection.extend({
model: AppModel
});
var AppView = Backbone.View.extend({
el: $('#app'),
newfield: $('#new-item'),
initialize: function(){
this.el = $(this.el);
},
events: {
'click #add-new': 'addItem',
'click .remove-item': 'removeItem',
'click #print-collection': 'printCollection'
},
template: $('#item-template').html(),
render: function(model){
var templ = _.template(this.template);
this.el.append(templ({
id: model.get('id'),
item: model.get('item')
}));
},
addItem: function(){
var NewModel = new AppModel({
id: _.uniqueId(),
item: this.newfield.val()
});
this.collection.add(NewModel);
this.render(NewModel);
},
removeItem: function(e){
var id = this.$(e.currentTarget).parent('div').data('id');
var model = this.collection.get(id);
this.collection.remove(model);
$(e.target).parent('div').remove();
},
printCollection: function(){
this.collection.each(function(model){
console.log(model.get('id')+': '+model.get('item'));
});
}
});
return {
start: function(){
new AppView({
collection: new AppCollection()
});
}
};
});
$(function(){ new App().start(); });
이드가 무엇을 기대합니까? [doc] (http://documentcloud.github.com/underscore/#uniqueId)에 따르면 'uniqueId' 함수는 정수를 반환하거나 접두어 앞에 접두사가 오는 정수를 전달하는 경우입니다. – Jack
@Jack - UID의 단일 정수 증분 대신 홀수의 출력에 관한 아주 간단한 질문이었습니다. – Fluidbyte
좋아, 나는 당신이 GUID와 같은 것을 기대하고 있다고 생각했고 왜 정수를 얻었는지 궁금해하고 있었다. 귀하의 질문에서 귀하가 왜 두 번 증분되는지 궁금해하는 것이 분명하지 않습니다. – Jack