2014-03-30 25 views
0

我有一個簡單的夾具:試圖做一個簡單的添加物品到夾具

App.User.FIXTURES = [ 
    { userid: 1, name: 'George', email: '[email protected]', bio: 'Lorem Ipsum', created: 'Jan 5, 2015' }, 
    { userid: 2, name: 'Tom', email: '[email protected]', bio: 'Lorem Ipsum 2', created: 'Jan 15, 2015' }, 
    { userid: 3, name: 'Mary', email: '[email protected]', bio: 'Lorem Ipsum 3', created: 'Jan 25, 2015' } 
]; 

我有一個簡單的提交:(片段)

App.AddController = Ember.ArrayController.extend({ 
    actions: { 
     save: function() { 
      App.User.createRecord({ id: 4, userid: 4, name: 'Created person', email: 'sdh', bio: 'my bio', created: '6543456' }); 

我認爲這是正確,因爲我再也沒有在createRecord上發生錯誤,但現在我得到一個錯誤,有什麼想法?還有一個步驟我錯過了將某物推入夾具?

Uncaught TypeError: Object function() { 
    if (!wasApplied) { 
     Class.proto(); // prepare prototype... 
    } 
    o_defineProperty(this, GUID_KEY, undefinedDescriptor); 
    o_defineProperty(this, '_super', undefinedDescriptor); 
+0

你是什麼改變導致錯誤?您需要調用保存在新記錄上才能將其保存回商店。 'var user = App.User.createRecord({id:4,userid:4,name:'Created person',email:'sdh',bio:'my bio',created:'6543456'}); user.save();'看到這部分指南:http://emberjs.com/guides/getting-started/creating-a-new-model/ – Sarus

+0

謝謝Sarus。對此完全陌生並試圖快速掌握這一點。 你的事情奏效了,但我得到一個錯誤,我猜想保存? XMLHttpRequest無法加載file:/// G:/ users。該請求被重定向到'file:/// G:/ users /',這對於需要預檢的跨源請求是不允許的。 – TrueHarlequin

+0

只是試圖將新項目放回商店,以便它更新我的UI中的列表。沒有JSON或花哨,只是一個本地的東西。 – TrueHarlequin

回答

0

Kingpin2k在調用UserModel本身的createRecord時是正確的,這是使用Ember Data的一種較老的方式。如果您使用的是最新版本,則應該從商店對象中調用createRecord

下面是它應該是什麼樣子:

DEBUG: ------------------------------- 
DEBUG: Ember  : 1.6.0-beta.1+canary.24b19e51 
DEBUG: Handlebars : 1.0.0 
DEBUG: jQuery  : 2.0.2 
DEBUG: ------------------------------- 

我建議審查「創建新模型實例」灰燼的部分入門指南:

App.AddController = Ember.ArrayController.extend({ 
    actions: { 
     save: function() { 
      //Create a new user 
      var user = this.store.createRecord('user',{ 
       id: 4, 
       userid: 4, 
       name: 'Created person', 
       email: 'sdh', 
       bio: 'my bio', 
       created: '6543456' 
      }); 
      // Saves the new model, but not needed if you're just using FIXTURES 
      // Making the call shouldn't throw any errors though and is used in the Guide 
      user.save(); 
      // Now you can find your record in the store 
      this.store.find('user', 4).then(function(user){ 
       console.info(user); 
      }); 
     } 
    } 
}); 

本上測試因爲他們覆蓋了這個話題:

http://emberjs.com/guides/getting-started/creating-a-new-model/

+0

謝謝Sarus,我剛剛使用ember.js網站的入門套件。 DEBUG:-------------------------------燼-1.4.0.js:3461 DEBUG:餘燼:1.4 0.0燼-1.4.0.js:3461 DEBUG:餘燼數據:1.0.0-beta.7.f87cba88燼-1.4.0.js:3461 DEBUG:車把:1.1.2餘燼-1.4.0.js :3461 DEBUG:jQuery:1.10.2 ember-1.4.0.js:3461 DEBUG:---------------------------- --- 這是我得到錯誤的保存位置:XMLHttpRequest無法加載文件:/// G:/ users。該請求被重定向到'file:/// G:/ users /',這對於需要預檢的跨源請求是不允許的。 – TrueHarlequin

+0

出於好奇,您是否在不使用Web服務器的情況下運行初學者軟件包?無論如何,因爲kingpin2k提到你不需要'save'調用來實際地將該項目保存到'store'中,儘管新記錄不會被保存。 – Sarus

+0

是的,沒有網絡服務器。我使用的是初學者軟件包,我猜測它在星期五是1.4.0,現在是1.5.0,但仍然是相同的錯誤。我把我的Javascript:http://jsfiddle.net/Harlequin/ZB7CQ/保存()帶回XMLHttpRequest錯誤,我只是想更新夾具,所以它有一個新的項目,然後刷新視圖。與刪除相同,但我想如果我找出保存刪除將遵循。 – TrueHarlequin

相關問題