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]
。所有非数组属性都可以正常工作。
希望我只是俯视一些容易!
其实,你并不需要 '新对象(' 只是, self.story()Tasks.push({ IsDone:假的, 说明:'Test description' }); 创建一个对象 – Damien
非常感谢您!我最终使用了@Damien建议并放弃了Object。 – mattruma