0

I am trying to push a new object into a child object array on my knockout object ... and I keep getting an object is not a function error.

My script looks like ...

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);
    };
}

If I look at my knockout context in Chrome I see my Tasks property as Array[0]. All the non-Array properties work just fine.

Hoping I am just overlooking something easy!

1 Answer 1

2

It's not a knockout's error. Just try in console a = new {a: false, b: true} and you will get such error.

You must set:

self.story().Tasks.push(new Object({
    IsDone: false,
    Description: 'Test description'
)});
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, you don't need the 'new Object('. Just that self.story().Tasks.push({ IsDone: false, Description: 'Test description' }); to create an object.
Thank you so much! I did end up using @Damien suggestion and just dropping the Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.