About observableArray.push

48 views
Skip to first unread message

lyan...@gmail.com

unread,
Apr 15, 2013, 5:22:00 PM4/15/13
to knock...@googlegroups.com
var MyCircle= {
            Friends: ko.observableArray([new Friend()]),           
            addFriend: function () { this.Plans.push(new Friend()); },
            removeFriend: function (Friend) { this.Plans.remove(Friend) },
        };

 ko.applyBindings(MyCircle);

Than I found in javascript console:

Friend      //this generated by " ko.observableArray([new Friend()])"

Array[1]    ////this generated by "MyCircle.Friends().Push([new Friend()])"

Array[1]   ////this generated by  "MyCircle.Friends().Push([new Friend()])"


Friend    //generated by addFriend



MyCircle.Friends().Push([new Friend()]) and addFriend: function () { this.Plans.push(new Friend()); },

the result is different! I can understand, but How can I add a viewmodel by code.....

Help!!!

lyan...@gmail.com

unread,
Apr 15, 2013, 5:28:01 PM4/15/13
to knock...@googlegroups.com, lyan...@gmail.com
Got it!  Call addFriend

Michael Best

unread,
Apr 15, 2013, 7:32:48 PM4/15/13
to knock...@googlegroups.com, lyan...@gmail.com
I'm guessing you meant to do this:

MyCircle.Friends.push(new Friend());

-- Michael

Hasen el Judy

unread,
Apr 17, 2013, 2:28:15 AM4/17/13
to knock...@googlegroups.com, lyan...@gmail.com
Why does your "addFriend" references "this.Plans"? It should push to "this.Friends".
Reply all
Reply to author
Forward
0 new messages