Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add object to observable array using knockout

For some reason i'm having trouble passing an object to observable array.

function CalendarViewModel() {
var self = this;

self.event = {
    name : ko.observable(""),
    adress : ko.observable(""),
    startTime : ko.observable(""),
    endTime : ko.observable("")
}

self.events = ko.observableArray([

])

self.addEvent = function (event) {

    self.events.push(event);

    alert(self.events.length)
    alert(self.events[0].name());
 }

my view:

 <fieldset class="add-event-fieldset">
            <div data-bind="with: event">
                <legend>Add Event</legend>
                <div style="text-align: center;">
                    <label class="title-label">What </label>
                </div>
                <div>
                    <label>Name: </label>
                    <input type="text" name="whatTxtBox" data-bind="value: name" />
                </div>
                <div>
                    <label>Where: </label>
                    <input type="text" name="whereTxtBox" data-bind="value: adress" />
                </div>
                <div style="text-align: center;">
                    <label class="title-label">When </label>
                </div>
                <div>
                    <label>start: </label>
                    <input type="text" id="startHourTxtBox" data-bind="value: startTime" />
                </div>
                <div>
                    <label>end: </label>
                    <input type="text" id="endHourTxtBox" data-bind="value: endTime" />
                </div>
            </div>

            <input type="hidden" name="" id="hiddenDay" />

            <button id="btnAddNewEvent" data-bind="click: $root.addEvent">+</button>
        </fieldset>

The alerts show that the array is always empty, please explain what i'm doing wrong thanks.

like image 405
Alan Budzinski Avatar asked Apr 27 '13 20:04

Alan Budzinski


People also ask

What is Ko observable ()?

An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted. The main advantage of KO is that it updates our UI automatically when the view model changes.

How do you clear a knockout observable array?

To clear an observableArray you can set the value equal to an empty array.


2 Answers

Your observable array usage e.g self.events.push(event); is correct (because observable array implements push), only your alerts are wrong.

The correct calls would be

alert(self.events().length)
alert(self.events()[0].name());

Because you need to call the observable array as a function like the regular ko.observable to get its underlying value the array itself.

However you are currently adding to whole CalendarViewModel to the array because the btnAddNewEvent is outside of yourwith binding so the current context will be your main view model.

One way to solve it: just add the self.event to the array, so:

self.addEvent = function() 
{ 
    self.events.push(self.event); 
    alert(self.events().length)
    alert(self.events()[0].name());
}

But this can cause problems later when you want to add another element because you will end up referencing the same element, so the right solution is copy the properties somewhere:

So I would create a constructor function for your event class:

var Event = function(data) {
    var self = this;
    self.name = ko.observable(data.name()),
    self.adress = ko.observable(data.adress()),
    self.startTime = ko.observable(data.startTime()),
    self.endTime = ko.observable(data.endTime())
}

And push a new event in the addEvent

self.events.push(new Event(self.event));
like image 162
nemesv Avatar answered Sep 23 '22 20:09

nemesv


Try

self.events().length

and

self.events()[0].name() 

instead.

like image 29
NilsH Avatar answered Sep 24 '22 20:09

NilsH