2012-08-16 4 views
0

이 백본 스크립트에는 단일 뷰와 컨트롤러로 작동하는 모델이 있으며 서버에서 데이터를 가져 오는 콜렉션 url: '/search/:term'이 있습니다.변경 모델 이벤트 콜렉션을 재설정 하시겠습니까?

var Items = Backbone.Collection.extend({ 
      initialize: function(terms){ 
      this.fetch(); 
     } 
      url: 'search/:term' 
    }); 

    var Controller = Backbone.Model.extend({ 
     defaults:{ 
     term: "" 
    }, 
     initialize: function(opts){ 

     this.on('change:term', function(term){ 
      console.log(this.get("term")); 
      // every time term changes i want to refresh the collection with the new data 
      // so it will fetch data from url:'search/ + term' 
     }); 

누군가 고맙습니다. } });

+0

성취하려는 것을 발견 할 수 있습니까? 어떤 dom-event를 듣고 자한다면, Backbone.View를 사용해야합니다. 백본 컨텍스트에서보기는 컨트롤러로 작동합니다. –

답변

0

백본 컨텍스트에서보기는 컨트롤러 (컨트롤러 유형 읽기 mvc)로 작동합니다. 이런 식으로하려고 할 수 있습니까?

YourView = Backbone.View.extend({ 
    events : { 
    'change #dom-id' : 'handleChange' //function to fire when you change the dom 
    }, 
    initialize: function(){ 
    this.collection.bind("reset", updateView); //when collection is done, updateView fires 
    }, 
    render: function(){ 
    //do your render logic here, use a template etc.. //initial rendering 
    }, 
    handleChange : function(changeEvent){ 
    //get value from dom-element, tell the collection to 
    //fetch again with the new term 
    }, 
    updateView : function(){ 
    //Code that fires when the collection is done fetching 
    } 
}); 
+0

어떻게 URL을 바꿀 수 있습니까! – user1551482

+0

컬렉션에 함수를 추가하십시오. 검색 : function (term) { this.url = 'search /'+ term; this.fetch(); } –

관련 문제