내 todos.js :컬렉션 길이가 0이지만 가져 오기가 성공한 이유는 무엇입니까?
var app = app || {};
(function() {
'use strict';
var Todos = Backbone.Collection.extend({
model: app.Todo,
url: '/api/todos'
});
app.todos = new Todos();
})();
앱 view.js
var app = app || {};
(function($){
'use strict';
app.AppView = Backbone.View.extend({
el: '.todoapp',
events : {
'keypress .new-todo': 'createOnEnter'
},
initialize: function() {
this.$input = this.$('.new-todo');
this.$list = $('.todo-list');
app.todos.fetch({
reset : true
});
this.render();
},
render: function(){
console.log(' len = ' + app.todos.length);
app.todos.each(function(todo){
this.renderTodo(todo);
}, this);
},
...
불러 온 데이터
[{
"_id": "5801",
"title": "testtitle123",
"completed": false,
"__v": 0
}, {
"_id": "58182",
"title": "testtitle126",
"completed": false,
"__v": 0
}, {
"_id": "5813",
"title": "testtitle127",
"completed": false,
"__v": 0
}]
이 (http://myip:8000/api/todos에서) 데이터를 가져 오는 데 성공했다. 그러나 콘솔에서, 길이는 나는 다음과 같은 코드를 시도 0
UPDATE
항상이다. 콘솔에는 'OK'또는 'error'가 기록되지 않습니다.
var app = app || {};
(function($){
'use strict';
app.AppView = Backbone.View.extend({
el: '.todoapp',
events : {
'keypress .new-todo': 'createOnEnter'
},
initialize: function() {
this.$input = this.$('.new-todo');
this.$list = $('.todo-list');
this.listenTo(app.todos, 'sync', this.render);
app.todos.fetch({
reset : true,
context: this,
success: function(col, res, op) {
console.log('OK');
},
error: function(col, res, op){
console.log('error');
}
});
},
render: function(){
console.log(' len = ' + app.todos.length);
app.todos.each(function(todo){
this.renderTodo(todo);
}, this);
},
renderTodo: function(todo) {
console.log('render :' + todo.title);
var todoView = new app.TodoView({ model : todo });
this.$list.append(todoView.render().el);
},
});
})(jQuery);
수신 된 데이터 무엇입니까? –
답변을 관련성이없고 혼란에 빠지게 만들기 때문에 질문에 대한 답변의 코드를 편집하지 마십시오. –
그 시점에서 다른 코드가 작동해야하므로 조사해야합니다. 'render'가 다른 곳에서 호출 되었기 때문입니까? –