2013-01-14 2 views
0

데이터베이스를 업데이트 한 다음 DOM을 업데이트 할 수 있도록 기능을 응용 프로그램에 추가하려고합니다. 데이터베이스는 잘 업데이트되지만 DOM은 업데이트되지 않습니다.백본 : 콜백 함수를 호출하지 않는 업데이트

내가 알고있는 것처럼
App.Views.Table = Backbone.View.extend({ 
    tagName: 'span', 
    initialize: function(options) { 
     this.model.on('change', this.render, this); 
     this.model.on('update', this.remove, this); 

     this.template = this.options.template; 
     this.url = this.options.url; 
    }, 
    events: { 
     'click .verify': 'verify', 
     'click .spam': 'spam', 
     'click .duplicate': 'duplicate' 
    }, 
    verify: function(e) { 
     id = e.currentTarget.id; 
     table = new App.Models.Table({ id: id }); 
     table.urlRoot = this.url; 
     table.fetch(); 
     table.toJSON(); 
     table.set('verified', 1); 
     table.save(); 

    }, 
    spam: function(e) { 
     ... 

    }, 
    duplicate: function(e) { 
      ... 
    }, 
    remove: function() { 
     this.$el.remove(); 
     console.log('hello'); 
    }, 
    retrieveTemplate: function(model) { 
     return _.template($('#' + this.template).html(), model); 
    }, 
    render: function() { 
     //console.log(this); 
     this.$el.html(this.retrieveTemplate(this.model.toJSON())); 
     return this; 
    } 
}); 

, this.model.on('update', this.remove, this); 내 제거 함수를 호출해야합니다 save 완료 : 여기 내보기의 일부입니다. 하지만 console.log을 얻지 못하고 내 DOM이 업데이트되지 않아서 콜백이 실행되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 튜토리얼을 따라 갔지만 튜토리얼에서는 모든 것이 잘 작동한다.

+0

내가 대신 update''의'create'을 시도 :

* 정말 아마도 당신이 뭘 하려는지 이해하지만,하지 않는 UPDATE *

이 시도 여전히 운이 없다. – sehummel

+0

table.toJSON(); - 코드 라인은 아무것도하지 않습니다. 반환하는 JSON으로 뭔가를해야합니다. – asgeo1

답변

2

update 이벤트가 없습니다. 난 당신이 또한 sync

http://backbonejs.org/#Events-catalog

"sync" (model, resp, options) — when a model has been successfully synced with the server.

내가 트리거지고 어떤 이벤트를 볼 수 all 이벤트를 사용했다 발견 디버깅에 도움이 히트 의미 생각합니다.

편집 : 디버깅 후

verify() 기능의 목표는 모델 검증 된 속성을 저장하는 것이 었습니다. 우리가 verify()

this.model.set('verified', 1); 
this.model.save(); 

에 대신 새로운 App.Model.Table를 만들고 table 변수로 설정의 변경이 필요하다고 수행합니다. table. save()를 수행하면 이전 모델 this.model 대신 table 모델을 저장하는 중이었습니다. 그렇기 때문에 this.model에 연결된 이벤트 처리기가 그물을 유발하는 이유입니다.

+0

'this.model.on ('sync', this.remove, this)'시도했는데 맞습니까? 작동하지 않습니다. – sehummel

+0

동기화하려고하면 오류가 발생했을 수 있습니까? 이 경우'error' 이벤트 – hajpoj

+0

에 가입해야합니다. 나는 그것을 시도 할 것이다. – sehummel

0

Backbone.js에는 "만들기"또는 "업데이트"이벤트가 없습니다. 이것이 remove() 콜백이 실행되지 않는 이유입니다.

Backbone에서 가능한 이벤트의 카탈로그는 http://backbonejs.org/#Events-catalog을 참조하십시오.

UPDATE는 :

더 밀접하게 코드를보고 한 후, 대답은 분명하다. 그들은 서로 다른 모델입니다 :

initialize: function(options) { 
    this.model.on('change', this.render, this); 
    this.model.on('sync', this.remove, this); 

table = new App.Models.Table({ id: id }); 
... 
table.save(); 

당신의 table 개체에서 발생하는 이벤트가 완전히 다른 모델 (this.model)에 바인딩 된 이벤트 핸들러를 트리거하지 않을 수 있습니다.

모델을 이미 만들었던 이유는 다른 모델 (테이블)을 만드는 이유는 무엇입니까? (이 모델)?

table = new App.Models.Table({ id: id }); 
table.on('sync', this.remove, this); 
+0

OK. 'this.model.on ('sync ', this.remove, this);'? – sehummel

+0

@sehummel 예, 성공 저장()에서 해고되어야합니다. 해고되지 않는 경우 오류가 발생했습니다 – asgeo1

+0

그것이 무엇 일지에 대한 생각? 어쩌면 모델을 보지 못했을까요? – sehummel

관련 문제