2014-07-16 2 views
1

Backbone.js로 빌드 한 내 webapp에서 부트 스트랩 모달을 열려고합니다. 이렇게하려면 github의 backbone.bootstrap-modal을 사용합니다. 이 코드를 사용하여 보여Backbone.js를 사용하여 Bootstrap Modal에 API 결과를로드하는 방법

var ListUsersModalView = Backbone.View.extend({ 
    render: function() { 
     this.$el.html($('#my-modal-template').html()); 
     return this; 
    } 
}); 

: : 지금 모델로 API 호출에서 모델의 목록을로드하려고

new Backbone.BootstrapModal({ 
    animate: true, 
    content: new ListUsersModalView() 
}).open(); 

간단한 모달을로드 할 때 쉽게 사용할 수 있습니다. 이제 다음 코드를 얻었습니다 :

var ListSupportersModalView = Backbone.View.extend({ 
    el: '#div-for-list-of-names', 
    render: function() { 
     var that = this; 
     var users = new UserCollection(); 
     users.fetch({ 
      success: function() { 
       var template = _.template($('#my-modal-template').html(), {users: users.models}); 
       that.$el.html(template); 
       return that; 
      } 
     }); 
    } 
    return that; 
}); 

이것은 작동하지 않으며 그 이유를 이해합니다. 로드 된 컬렉션을 포함하여 this을 반환해야합니다. 불행하게도 사용자는 비동기 적으로 가져 오므로 마지막 return that;에는 usermodels이 포함되지 않습니다.

질문은 지금이다; 어떻게 해결할 수 있을까요? 어떻게 모델을 API 호출에서 모달로로드 할 수 있습니까? 모든 팁을 환영합니다!

답변

3

내 의견으로는 Jquery의 연기 된 객체가 가장 좋은 방법입니다.

이렇게하면 나중에 코드에서 처리 할 "약속"을 반환 할 수 있습니다. 일단 약속을 해결하거나 거절하면 데이터를 표시하거나 거부 한 경우 오류를 표시 할 수 있습니다. 에 대한

var listUsersModalView = new ListUsersModalView(); 

//get the users and this will return the prmoise; 
var listUsersModalViewPromise = listUsersModalView.getUsers(); 


//set up listen for when the promise is resolved/rejected 
$.when(listUsersModalViewPromise).done(function() { 


    //only change i have made is to pass the listUserModalView that has already 
    //been initialized as this will now have a fetched user collection 
    new Backbone.BootstrapModal({ 
     animate: true, 
     content: listUsersModalView 
    }).open(); 
}).fail(function() { 
    //do something here to show that an error occured? 
}); 
+0

감사 부트 스트랩 모달을 설정할 때

당신이 구조 조정을 수있는 방법의 간단한 예는 다음이

var ListSupportersModalView = Backbone.View.extend({ users: null, // This tells the view which element is its container // remove this if you don't need it. el: '#div', initialize: function() { this.users = new UserCollection(); }, //moved this to a it's own function so that it can //return the promise for action it is completing getUsers: function() { var defer = $.Deferred(); this.users.fetch({ success: function() { //users should now be a full collection so //resolve the promise defer.resolve(); }, error: function() { //users fetch failed so reject the promise defer.reject(); } }); //return the promise from the deferred object var promise = defer.promise(); return promise; }, render: function() { var template = _.template($('#my-modal-template').html(), { users: this.users.models }); this.$el.html(template); return this; } }); 

을 사용하도록 (자세한 여기 http://api.jquery.com/category/deferred-object/ 그것에 대해 읽을 수 있습니다) 대답! 나는 당신의 대답을 구현했습니다. 그것은 모두 아주 오래 된 것 같습니다. 모델을 올 바르고 모달을 엽니 다. 그것은 단지 모달에 아무것도 표시하지 않습니다. 나는 수동 html behine'content :'를 삽입했다. 나는 new backbone.BootstrapModal (등등)을 실행하기 직전에'console.log (listUsersModalView)'를 수행했으며, usermodels을 포함하고 있습니다. 단지하지 않는 것은 내용을 표시하는 것입니다. 어떻게 든 그것을 HTML로 변환 할 수 있습니까? 필자는 필자가 솔루션에 너무 가깝기 때문에 필사적이다. 마지막 팁 하나는 대단히 환영한다! – kramer65

+0

해결 : 나 역시 el : '# div-for-list – kramer65

+0

아, 죄송합니다. 예제 코드에서 예제 코드를 복사 해 두었습니다. 고맙게 생각합니다. – Quince

관련 문제