Hi,
Passing array as an argument to ObservableArray push does not work as expected. Would it not make sense to check if arguments contains a single array and use it as a parameter to push.apply(this, items). Here is a sample.
Passing array as an argument to ObservableArray push does not work as expected. Would it not make sense to check if arguments contains a single array and use it as a parameter to push.apply(this, items). Here is a sample.
var
kendo = window.kendo,
CHANGE =
"change"
,
isArray = $.isArray,
push = [].push,
stringify = JSON.stringify;
kendo.data.ObservableArray.prototype.push =
function
() {
var
index =
this
.length;
var
items = arguments;
// if no args passed return length
if
(items.length === 0)
{
return
index; }
if
(items.length === 1 && isArray(items[0])) {
items = items[0];
}
items =
this
.wrapAll(items);
var
result = push.apply(
this
, items);
this
.trigger(CHANGE, {
action:
"add"
,
index: index,
items: items
});
return
result;
};
8 Answers, 1 is accepted
0
Hi Maxim,
The behavior you observe is by design - the built-in push array method will not splatter the passed array into different items; this would make pushing arrays objects impossible.
Regards,
Petyo
Telerik
The behavior you observe is by design - the built-in push array method will not splatter the passed array into different items; this would make pushing arrays objects impossible.
Regards,
Petyo
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Maxim
Top achievements
Rank 1
answered on 28 Apr 2014, 02:14 PM
Hi Petyo,
I see your point. However that means that there is no way to add or replace item(s) in bulk in ObservableArray since there is no set or add method. For example I want to load ObservableArray when ajax call returns.
PS: Yes I know I can work directly with view model and use set but that feels clunky at best.
Thanks,
Max
I see your point. However that means that there is no way to add or replace item(s) in bulk in ObservableArray since there is no set or add method. For example I want to load ObservableArray when ajax call returns.
PS: Yes I know I can work directly with view model and use set but that feels clunky at best.
Thanks,
Max
0
Hi Maxim,
You can use the push.apply approach on the observable array:
Regards,
Petyo
Telerik
You can use the push.apply approach on the observable array:
var
vm = kendo.observable({
data:
new
kendo.data.ObservableArray([])
});
vm.data.push.apply(vm.data, [ 1, 2, 3]);
console.log(vm.get(
"data"
));
Regards,
Petyo
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Maxim
Top achievements
Rank 1
answered on 28 Apr 2014, 02:59 PM
Hi Petyo,
Yes I can use that but doing so will trigger a change event for every item in source array... Probably will cause re rendering when bound to a widget each time an item is added. There should be a way to add multiple items in bulk mode triggering only one change event upon completion.
Thanks,
Maxim
Yes I can use that but doing so will trigger a change event for every item in source array... Probably will cause re rendering when bound to a widget each time an item is added. There should be a way to add multiple items in bulk mode triggering only one change event upon completion.
Thanks,
Maxim
0
Hello Maxim,
No, it won't - please check this jsbin. The change event is triggered once.
Regards,
Petyo
Telerik
No, it won't - please check this jsbin. The change event is triggered once.
Regards,
Petyo
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Maxim
Top achievements
Rank 1
answered on 28 Apr 2014, 04:49 PM
Hi Petyo,
Kudos to you. That works well.
Thank,
Maxim
Kudos to you. That works well.
Thank,
Maxim
0
Xavier
Top achievements
Rank 1
answered on 20 Aug 2014, 06:29 AM
Where can I find the documentation for vm.data.push.apply ?
Why is vm.data used twice in vm.data.push.apply(vm.data, [ 1, 2, 3]); ?
Is there a command to empty the array?
Thanks.
Why is vm.data used twice in vm.data.push.apply(vm.data, [ 1, 2, 3]); ?
Is there a command to empty the array?
Thanks.
0
Maxim
Top achievements
Rank 1
answered on 20 Aug 2014, 10:56 AM
This is built in JS function that enables you to call any function using specific args and this context.
See this:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
See this:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply