2013-09-06 1 views
8

컬렉션 가져 오기에서 모델의 구문 분석 기능을 방지하는 방법은 무엇입니까?백본 - 가져 오기 후 컬렉션의 각 모델을 구문 분석하지 않습니다.

$(function() { 
    var Task = Backbone.Model.extend({ 
     url : function() { 
       return this.urlRoot + this.id; 
     }, 
     urlRoot: 'index.php?c=light&a=list_tasks_bb&ajax=true&task=', 
     parse: function (response, options) { 
      console.log(options); 
      console.log(response); 
      return response; 
     } 
    }); 

    var TaskList = Backbone.Collection.extend({ 
     model: Task, 
     url: 'index.php?c=light&a=list_tasks_bb&ajax=true', 

     initialize: function() { 

     }, 
     parse: function (response, options) { 
      return response.tasks; 
     } 
    }); 

    var Light = Backbone.Router.extend({ 
     el: $('#light'), 
     routes: { 
      "tasks/:id": "taskAction", 
      "*page": "defaultAction", 
     }, 

     initialize: function() { 
      _this = this; 
      this.tasks = new TaskList(); 
      this.users = new UserList(); 
     }, 

     taskAction: function(id) { 
      this.task = new Task({id: id}); 
      $.when (
       this.task.fetch() 
      ).then(function(zzz) { 
       new TaskView({model: _this.task}).render(); 
      }); 
     }, 
     defaultAction: function(page) { 
      $.when (
       this.tasks.fetch(), 
       this.users.fetch() 
      ).then (function() { 
       new TaskListView({collection: _this.tasks}).render(); 
      }); 
     } 
    }); 
}); 

나는 ajax 가져 오기를 통해 얻은 하나의 모델과 컬렉션이 있습니다. 나는 백엔드를 변경할 수있는 기회를 가질 수 없기 때문에, 작업 목록의 JSON 구조는 다음과 같습니다

"contents": {}, 
"current": false, 
"errorCode": 0, 
"errorMessage": "", 
"u": 4, 
"tasks": [{ 
    "id": "12250", 
    "t": "ZZZ", 
    "cid": "24", 
    "c": "2013-08-22 11:36:32", 
    "dd": "02.09.2013", 
    "sd": "02.09.2013", 
    "pr": "300", 
    "pid": "0", 
    "atid": "1:4", 
    "s": 0, 
    "dl": "" 
}, { 
    "id": "12307", 
    "t": "ZZZ", 
    "cid": "42", 
    "c": "2013-08-28 11:14:44", 
    "dd": "05.09.2013", 
    "sd": "28.08.2013", 
    "pr": "200", 
    "pid": "0", 
    "atid": "1:4", 
    "s": 0, 
    "dl": "" 
}, { 
    "id": "12326", 
    "t": "ZZZ", 
    "cid": "2", 
    "c": "2013-08-29 09:55:34", 
    "dd": "31.08.2013", 
    "sd": "29.08.2013", 
    "pr": "200", 
    "pid": "0", 
    "atid": "1:4", 
    "s": 0, 
    "dl": "" 
}], 
"events": [] 

이 내가 수집, 구문 분석을 사용하고 있습니다 이유입니다. 이 단계에서는 모든 것이 좋습니다. 하나의 작업에 대한 JSON 구조는 다음과 같습니다

"contents": {}, 
"current": false, 
"errorCode": 0, 
"errorMessage": "", 
"u": 4, 
"tasks": [{ 
    "id": "12250", 
    "t": "ZZZZ", 
    "cid": "24", 
    "c": "2013-08-22 11:36:32", 
    "dd": "02.09.2013", 
    "sd": "02.09.2013", 
    "pr": "300", 
    "pid": "0", 
    "atid": "1:4", 
    "text": "XXXXX", 
    "s": 0, 
    "dl": "" 
}], 
"comments": [{ 
    "id": "48178", 
    "text": "CCCC", 
    "cid": "4", 
    "con": "23.08.2013" 
}], 
"events": [] 

그래서 난 후 하나의 작업을 가져 오기 위해 "task.fetch) ("다시 구문 분석이 필요합니다. 모델에서 구문 분석 함수를 추가 한 후에 컬렉션 가져 오기를 시작하기 전까지 컬렉션 파싱을 시작 했으므로 콜렉션을 구문 분석 한 후에 각 모델에 대해 올바른 모델 데이터가 있지만 모델 분석 콜백을 다시 수행했기 때문에 제대로 작동합니다.

이 문제를 해결할 올바른 방법이 있습니까? 아니면 백엔드를 변경하려고합니까?

PS 물론, 나는 같은 것을 수행 할 수 있습니다

if(response.tasks) { 
    return response.tasks[0]; 
    } else { 
    return response; 
    } 

을하지만 그것이 올바른 해결책이 아니다 생각합니다.

답변

23

creating models for insertion in a collection 일 때 Backbone은 미래의 컬렉션을 모델 생성자의 옵션으로 전달하여 모델 생성자에게이 옵션을 parse으로 전달합니다. 이 속성을 확인하고 필요에 따라 구문 분석을 중단 할 수 :

var Task = Backbone.Model.extend({ 
    parse : function(response, options){ 
     if (options.collection) return response; 
     return response.tasks[0]; 
    } 
}); 
var TaskList = Backbone.Collection.extend({ 
    model: Task, 
    parse : function(response){ 
     return response.tasks; 
    } 
}); 

그리고 http://jsfiddle.net/nikoshr/dfMgR/

+2

감사합니다 데모를! 너는 나를 많이 구해줬다. –

+1

재 파싱 문제인 구원자를 구하는 데 하루가 걸렸습니다. – topless