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 badges3 Answers
Reset to default 3Use
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
版权声明:本文标题:javascript - Knockout.js: adding elements to observable-array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745243266a2649438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论