What is the best way of cloning/copying an observablearray in knockoutJS?

后端 未结 4 754
终归单人心
终归单人心 2020-12-13 12:41

Question says it all really. I want to copy an observable array to another in KnockoutJS.

4条回答
  •  一生所求
    2020-12-13 13:03

    I was faced with the same task; to clone an observable array. The only way I could figure out how to do it, is to convert the observable to an JS object, then convert that object to an observable object. The following function requires KnockoutJS mapping plugin: http://knockoutjs.com/documentation/plugins-mapping.html

    function cloneObservable(observableObject) {
        return ko.mapping.fromJS(ko.toJS(observableObject));
    }
    

    Hope this helps

提交回复
热议问题