2014-07-17 8 views
1

두 번째로 나는이 질문을하고 있는데, 처음에는 텍스트가 이해하기 쉽지가 않아서 미안합니다. 다음은 개선 된 동일한 질문입니다.백본 (동기화) 업데이트 이벤트

나는 PageableCollection을 가지고 있는데, 문제없이 데이터가로드됩니다! 테이블의 라인 (backgrid, 백본의 확장 기능을 사용하고 있습니다)을 편집 할 수 있습니다 !! 하지만 편집 후 Enter 버튼을 누르면 아무 일도 일어나지 않습니다! 서버가 호출되지 않습니다. 서버에 대한 호출 AJAX를 기다리지 만 발생하지 않습니다.

동기화를 무시할 수있는 방법을 찾았지만 수정 후 "okay"또는 "error updating"이라고 말하고 싶습니다. 울부 짖는 소리

코드 :

var Territory = Backbone.Model.extend({}); 

var PageableTerritories = Backbone.PageableCollection.extend({ 
    model: Territory, 
    url: "linhas/results", 
    state: { 
     pageSize: 9 
    }, 
    mode: "client", 
    sync: function (method, model, options){ 
     return Backbone.sync(method, model, options); 
    } 
}); 


var pageableTerritories = new PageableTerritories(), 
    initialTerritories = pageableTerritories; 

function createBackgrid(collection){ 
    var columns = [{ 
     name: "id", // The key of the model attribute 
     label: "ID", // The name to display in the header 
     editable: false, // By default every cell in a column is editable, but *ID* shouldn't be 
     // Defines a cell type, and ID is displayed as an integer without the ',' separating 1000s. 
     cell: Backgrid.IntegerCell.extend({ 
      orderSeparator: '' 
     }) 
    }, { 
     name: "name", 
     label: "Name", 
     // The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string 
     cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up 
    }, { 
     name: "pop", 
     label: "Population", 
     cell: "integer" // An integer cell is a number cell that displays humanized integers 
    }, { 
     name: "url", 
     label: "URL", 
     cell: "uri" // Renders the value in an HTML <a> element 
    }]; 
    if ($(window).width() < 768){ 
     //okendoken. removing URL-column for screens smaller than 768px 
     columns.splice(3,1) 
    } 
    var pageableGrid = new Backgrid.Grid({ 
     columns: columns, 
     collection: collection, 
     footer: Backgrid.Extension.Paginator.extend({ 
      //okendoken. rewrite template to add pagination class to container 
      template: _.template('<tr><td colspan="<%= colspan %>"><ul class="pagination"><% _.each(handles, function (handle) { %><li <% if (handle.className) { %>class="<%= handle.className %>"<% } %>><a href="#" <% if (handle.title) {%> title="<%= handle.title %>"<% } %>><%= handle.label %></a></li><% }); %></ul></td></tr>') 
     }), 
     className: 'table table-striped table-editable no-margin' 
    }); 
    $("#table-dynamic").html(pageableGrid.render().$el); 


} 

답변

3

당신은 셀을 편집 한 후 저장 트리거 할 필요가있다. Territory 모델의 변경 이벤트를 듣는 것만으로 간단하게 수행 할 수 있습니다. 나는 다음을 사용하고있다 :

var MyModel = Backbone.Model.extend({ 
    initialize : function() { 
     this.on('change', function(model, options) { 
      // Prevent save on update 
      if (options.save === false) 
       return; 

      model.save(_.clone(model.attributes), {}); 
     }); 
    }, 
    // Process and remove model unrelated stuff from server response 
    parse : function(resp, options) { 
     // pure model fetch 
     if (!resp.data && !resp.notifications) 
      return resp; 

     // process notifications 
     if (resp.notifications && resp.notifications.length) 
      processNotifications(resp.notifications); 

     return resp.data || {}; 
    }, 
    rollBack : function() { 
     // Rollback to old data 
     this.set(this.previousAttributes(), {save: false}); 
     // Trigger cell rendering 
    }, 
    save : function(attrs, options) { 
     options || (options = {}); 

     options.success = function(model, resp, options) { 
      // Show optional success messages here if needed and 
      // not in response from server 
     }; 

     options.error = function(model, xhr, options) { 
      var resp = JSON.parse(xhr.responseText); 
      model.parse(resp, options); 
      // Rollback to old data 
      model.rollBack(); 
      model.trigger('backgrid:error'); 
     }; 

     // We get the correct data back from the server - prevent multiple saves 
     options.save = false; 
     options.data = JSON.stringify(attrs); 

     Backbone.Model.prototype.save.call(this, attrs, options); 
    } 
}); 

이 구현은 서버로부터 모든 통지를 얻는다. 따라서 알림은 model.parse 메서드에서 필터링됩니다. 알림이있는 경우 데이터는 응답의 속성 데이터에 있습니다. 그러나 js의 의견을 표시하는 경우 options.success 및 options.error에 구현하십시오.

+0

그게 전부입니다! 공장! 고맙습니다 (: – Mauricionik