2013-02-13 2 views
0

나는 다음과 같은 JSON 데이터가 /products/index.json/입니다하나의 백본 모델을 다른 모델과 어떻게 연관 시키나요?

URL이 제품의 2 페이지를 검색 할 /products/index.json 위의 데이터가 검색

{ 
    "results":[ 
     { 
     "Product":{ 
      "id":"1", 
      "short_name":"Infra - 2200 CAS Sma SIMO onl [DAS.1.1]", 
      "serial_number":"DAS.1.1", 
      "created_by":"Wesley Jace Tan", 
      "modified_by":"Wesley Jace Tan", 
      "created":"2013-02-11 07:58:20", 
      "modified":"2013-02-11 07:58:20", 
      "full_name_type":"2200", 
      "full_name_cas_stk":"CAS", 
      "full_name_size":"Small", 
      "full_name_simo_mimo":"SIMO only", 
      "full_name_product_code":"(2961-737)", 
      "uom":"lot", 
      "material":"Infra" 
     }, 
     "Price":[ 
      { 
       "id":"1", 
       "product_id":"1", 
       "source_file":"LTE Test File.xls", 
       "for_financial_year":"FY12_13", 
       "created_by":"Wesley Jace Tan", 
       "modified_by":"Wesley Jace Tan", 
       "created":"2013-02-11 07:58:20", 
       "modified":"2013-02-11 07:58:20", 
       "gross_unit":"50.00", 
       "gross_total_value":"0.00", 
       "gross_total_formula":"=K12*J12", 
       "incentive_value":"5", 
       "incentive_formula":"5", 
       "net_price_unit":"0.00", 
       "net_price_total_value":"0.00", 
       "net_price_total_formula":"=N12*J12" 
      } 
     ] 
     }, 
     { 
     "Product":{ 
      "id":"2", 
      "short_name":"Infra - 2200 CAS Sma SIMO to [DAS.1.2]", 
      "serial_number":"DAS.1.2", 
      "created_by":"Wesley Jace Tan", 
      "modified_by":"Wesley Jace Tan", 
      "created":"2013-02-11 07:58:20", 
      "modified":"2013-02-11 07:58:20", 
      "full_name_type":"2200", 
      "full_name_cas_stk":"CAS", 
      "full_name_size":"Small", 
      "full_name_simo_mimo":"SIMO to MIMO Retrofit", 
      "full_name_product_code":"(2961-737)", 
      "uom":"lot", 
      "material":"Infra" 
     }, 
     "Price":[ 
      { 
       "id":"2", 
       "product_id":"2", 
       "source_file":"LTE Test File.xls", 
       "for_financial_year":"FY12_13", 
       "created_by":"Wesley Jace Tan", 
       "modified_by":"Wesley Jace Tan", 
       "created":"2013-02-11 07:58:20", 
       "modified":"2013-02-11 07:58:20", 
       "gross_unit":"11.00", 
       "gross_total_value":"0.00", 
       "gross_total_formula":"=K13*J13", 
       "incentive_value":"24", 
       "incentive_formula":"24", 
       "net_price_unit":"0.00", 
       "net_price_total_value":"0.00", 
       "net_price_total_formula":"=N13*J13" 
      } 
     ] 
     }, 
     { 
     "Product":{ 
      "id":"3", 
      "short_name":"Infra - 2200 CAS Sma Full MIM [DAS.1.3]", 
      "serial_number":"DAS.1.3", 
      "created_by":"Wesley Jace Tan", 
      "modified_by":"Wesley Jace Tan", 
      "created":"2013-02-11 07:58:20", 
      "modified":"2013-02-11 07:58:20", 
      "full_name_type":"2200", 
      "full_name_cas_stk":"CAS", 
      "full_name_size":"Small", 
      "full_name_simo_mimo":"Full MIMO", 
      "full_name_product_code":"(2961-737)", 
      "uom":"lot", 
      "material":"Infra" 
     }, 
     "Price":[ 
      { 
       "id":"3", 
       "product_id":"3", 
       "source_file":"LTE Test File.xls", 
       "for_financial_year":"FY12_13", 
       "created_by":"Wesley Jace Tan", 
       "modified_by":"Wesley Jace Tan", 
       "created":"2013-02-11 07:58:20", 
       "modified":"2013-02-11 07:58:20", 
       "gross_unit":"12.00", 
       "gross_total_value":"0.00", 
       "gross_total_formula":"=K14*J14", 
       "incentive_value":"5", 
       "incentive_formula":"5", 
       "net_price_unit":"0.00", 
       "net_price_total_value":"0.00", 
       "net_price_total_formula":"=N14*J14" 
      } 
     ] 
     } 
    ] 
} 

URL을 응답이 페이지 : 2

내 목표는 백본 가격 모델이있는 백본 제품 모델을 사용하는 것입니다.

따라서 위 결과 배열의 각 항목은 Product 및 Price 백본 모델 인스턴스를 생성합니다.

Backbone 모델간에 연관성을 갖는 것이 이상적입니까?

어떻게 달성 할 수 있습니까?

UPDATE

내가 권장 솔루션을 시도 후에 나는 다음과 같은 오류 메시지가 나타납니다. 잡히지 않은 TypeError : Object 함수() {return c.apply (this, arguments)}에 'fetch'메서드가 없습니다.

답변

3

collection.parse이 필요합니다. 샘플 코드 :

var Products = Backbone.Collection.extend({ 
      model: Product, 
      url: '/products/index.json', 
      parse: function(response) { 
       return _.map(response.results, // map each result to a product 
         function(result){ 
          var product = result.Product; 
          product.price = new Price(result.Price); // create a price model 
          return product; // Backbone will convert the product to model 
         }); 
      } 
     }); 
+0

http://jsfiddle.net/QykCc/는 최신 코드입니다. 이제는 Uncaught TypeError를 보여줍니다 : Object function() {return c.apply (this, arguments)}에는 'fetch'메서드가 없습니다. –

+0

제 의견을보세요 (http://stackoverflow.com/questions/14845972/how- to-use-backbone-collection-to-fetch-this-json-result # comment20820858_14846064)를 사용하십시오. –

관련 문제