admin管理员组

文章数量:1415644

I have a problem with loading an element into my items Observable-Array - with an event.

ViewModel = (function () {
    var 
        items = ko.observableArray([]),

        removeItems = function (element) {
            items.remove(element);
        },
        saveAll = function () {
            return ko.toJS(items);
        },
        addItem = function (element) {
            items.push(element);
            return false;  // no Page-Reload after button-klick
        };

    return {
        Items: items,
        // i call addItem with a dummy object (for testing)
        clickSave: addItem(new Customer(1, "Tfsd", "Tfsd"))
    };
})();

(fiddle)

Why is the addItem function called, without even clicking the button? is it because of the () at the end of the function?

    addItem = function (element) {
        items.push(element);
        return false;  // no Page-Reload after button-click
    };

what can i do to make this for the event only? Or is my problem somewhere else?

I have a problem with loading an element into my items Observable-Array - with an event.

ViewModel = (function () {
    var 
        items = ko.observableArray([]),

        removeItems = function (element) {
            items.remove(element);
        },
        saveAll = function () {
            return ko.toJS(items);
        },
        addItem = function (element) {
            items.push(element);
            return false;  // no Page-Reload after button-klick
        };

    return {
        Items: items,
        // i call addItem with a dummy object (for testing)
        clickSave: addItem(new Customer(1, "Tfsd", "Tfsd"))
    };
})();

(fiddle)

Why is the addItem function called, without even clicking the button? is it because of the () at the end of the function?

    addItem = function (element) {
        items.push(element);
        return false;  // no Page-Reload after button-click
    };

what can i do to make this for the event only? Or is my problem somewhere else?

Share Improve this question edited Jun 4, 2012 at 13:53 Tomalak 339k68 gold badges546 silver badges635 bronze badges asked Jun 4, 2012 at 13:44 Thomas DeutschThomas Deutsch 2,5642 gold badges28 silver badges36 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Use

return {
    Items: items,
    clickSave: addItem
};

Why is the addItem function called, without even clicking the button? is it because of the () at the end of the function?

Yes.

Do this instead:

return {
    Items: items,
    clickSave: function() {
        addItem(
            new Customer( 
                items().length + 1, // or whatever you use to determine new IDs
                $("#inputVorname").val(),
                $("#inputNachname").val()
            )
        );
    }
};

This should work if you want the new item to be created always the same.

return {
    Items: items,
    clickSave: addItem.bind(null, new Customer(1, "Tfsd", "Tfsd"))
};

本文标签: javascriptKnockoutjs adding elements to observablearrayStack Overflow