This is a migrated thread and some comments may be shown as answers.

passing array to ObservableArray push

8 Answers 953 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Maxim
Top achievements
Rank 1
Maxim asked on 25 Apr 2014, 08:58 PM
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.

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

Sort by
0
Petyo
Telerik team
answered on 28 Apr 2014, 08:41 AM
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
 
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
0
Petyo
Telerik team
answered on 28 Apr 2014, 02:47 PM
Hi Maxim,

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
0
Petyo
Telerik team
answered on 28 Apr 2014, 04:03 PM
Hello Maxim,

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
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.
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
Tags
MVVM
Asked by
Maxim
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Maxim
Top achievements
Rank 1
Xavier
Top achievements
Rank 1
Share this question
or