2013-07-25 94 views
0

我试图将新对象推入我的淘汰对象中的子对象数组中......并且我一直收到object is not a function错误。将新对象推入淘汰对象的子属性数组

我的脚本看起来像......

function PageViewModel() { 

    var self = this; 

    self.story = ko.observable(); 
    self.stories = ko.observableArray(); 

    self.addTask = function() { 
     // this is where the error is occurring 
     self.story().Tasks.push(new { IsDone: false, Description: 'Test description' }); 
    }; 

    self.getStories = function() { 
     return $.ajax({ 
      type: 'GET', 
      url: '@Url.Action("List", "Stories")', 
      success: getStoriesSuccess 
     }); 
    }; 

    function getStoriesSuccess(data) { 
     var mapping = {}; 
     ko.mapping.fromJS(data.Stories, mapping, self.stories); 
    } 

    self.init = function() { 
     self.getStories(); 
     ko.applyBindings(self); 
    }; 
} 

如果我看在Chrome我的淘汰赛方面,我看到我的Tasks财产Array[0]。所有非数组属性都可以正常工作。

希望我只是俯视一些容易!

回答

2

这不是一个淘汰赛的错误。只需在控制台a = new {a: false, b: true}中尝试一下,就会发生这样的错误。

您必须设置:。

self.story().Tasks.push(new Object({ 
    IsDone: false, 
    Description: 'Test description' 
)}); 
+1

其实,你并不需要 '新对象(' 只是, self.story()Tasks.push({ IsDone:假的, 说明:'Test description' }); 创建一个对象 – Damien

+0

非常感谢您!我最终使用了@Damien建议并放弃了Object。 – mattruma