2013-10-22 81 views
0

我想知道什麼是加載數據到商店的正確方法。我有我的路由器定義爲:通過json轉換將數據加載到店鋪的ember數據版本1.0

this.resource('analyticsTemplates', { path: 'templates' }); 
this.resource('analyticsTemplate', { path: 'templates/:analytics_template_id' }); 

和我的「analyticsTemplates」的路線,我已經定義,列出一個表,所有的模板與形式開闢了一個模式對話框的添加按鈕車把模板,當提交應該添加一個新的記錄到商店並更新我的表。現在我的表單使用文件輸入字段,所以我沒有使用

this.get('model').save() 

它正確地同步存儲。相反,當表單提交時,它會將表單發佈到隱藏的iframe,然後由表示當前添加的記錄的json數據填充此iframe的正文內容。返回的數據看起來是這樣的:

{ 
    "analyticsTemplate": { 
     "id": 6, 
     "name": "deee", 
     "description": "asdasdasd", 
     "csFileName": "commandScript.txt", 
     "csLastUpdatedBy": "Deewendra Shrestha", 
     "csLastUpdated": "2013-10-22T18:00:54Z", 
     "dateCreated": "2013-10-22T18:00:54Z", 
     "lastUpdated": "2013-10-22T18:00:54Z", 
     "createdBy": "Deewendra Shrestha", 
     "lastUpdatedBy": "Deewendra Shrestha", 
     "parameters": { 
      "homeDir": { 
       "category": "global", 
       "description": "Where is the analytics folder", 
       "name": "homeDir", 
       "value": "Q:/Reckitt Benckiser/2013/RB_Steroid/Analytics/IntergerateTest_DONOTUSE" 
      }, 
      "historyDir": { 
       "category": "global", 
       "description": "Where R image will be saved", 
       "name": "historyDir", 
       "value": "Rhistory" 
      }, 
      "Sourcefolder": { 
       "category": "type 1", 
       "description": "Where are source codes and functions are saved", 
       "name": "Sourcefolder", 
       "value": "J:/_MRO/Analytics/Analytics_R_Source_Scripts" 
      }, 
      "resultsDir": { 
       "category": "global", 
       "description": "Where are results stored", 
       "name": "resultsDir", 
       "value": "results" 
      }, 
      "dataDir": { 
       "category": "opt", 
       "description": "Where is raw csv stored", 
       "name": "dataDir", 
       "value": "Export" 
      }, 
      "testParam": { 
       "category": "global", 
       "description": "some long sentence repeated multiple times. some long sentence repeated multiple times. some long sentence repeated multiple times.", 
       "name": "testParam", 
       "value": "some long sentence repeated multiple times. some long sentence repeated multiple times.some long sentence repeated multiple times." 
      } 
     } 
    } 
} 

我想在我的控制器兩兩件事:

this.get('store').push('analyticsTemplate',jsonData["analyticsTempalte"]); 

也:

var newRecord = this.get('store').createRecord('analyticsTemplate'); 
for(var key in jsonData["analyticsTemplate"]){ 
    o.set(key,jsonData["analyticsTemplate"][key]); 
} 

使用方法中的任何一個上面我可以看到新表中列出的記錄,但當我點擊記錄鑽取到「analyticsTemplate」路線(類似於從posts-> post),我得到js錯誤:

parameters.forEach(function (parameter) { 
    discreteCategories[parameter.get('category')] = 1; 
}); 

我已經定義了一個DS.transform將參數轉換爲ArrayProxy,我認​​爲這個轉換沒有得到執行時,我加載記錄,因爲我已經做了,因此子路線不知道如何處理參數。那麼如何在這種情況下應用轉換?

我希望我可以申請:

this.get( '模式')重新加載()

但似乎這樣的作品在ObjectController語境而不是ArrayController。任何想法/替代品的朋友?

回答

0

Jeez餘燼使事情變得如此簡單,我試圖找到困難的方式。因此,所有我需要做的就是在我的控制器操作添加這些:

ajaxFileUpload: { 
    var self = this; 
    //do upload stuff, add record to DB 
    ..... 
    var analyticsTemplateData = $.parseJSON(responseText); 
    var id = analyticsTemplateData["analyticsTemplate"]["id"]; 
    var store =self.get('store'); 
    store.find('analyticsTemplate',id); 
} 

這將耗費服務器的請求,我認爲這是不壞,並且能夠完成任務。但是,如果你知道另一種方式來加載數據,不會花費額外的服務器電話,那麼我就是所有人。謝謝。