最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ObservableArray.RemoveAll() empties the concated objects in a different variable - Stack Overflow

programmeradmin1浏览0评论

In my KnockoutJS view model logic I have an observable array of currently displayed objects (project view models): self.currentProjects. Depending on an item filter setting (user chooses project types dynamically) self.currentProjects holds different objects that a UI is bound to.

At some point, where I need to load different projects into the view, I need to empty the currentProjects and put different set of models into it. I do this by calling self.currentProjects.removeAll(); The problem is when I call it, another observable array self.ProjectModels that should hold the reference to the same set of models also appears to be emptied up. How should I manage objects in an observable array, so that removal from one array doesn't cause the other to bee empty.

Here is a manipulation related code:

    //Another menu item is selected
    self.selectItem = function(newId) {
        self.selectedItemId(newId);
        self.currentProjects.removeAll(); //This is the point where self.ProjectModels also looses model references
        var someProjectsLoaded = false;
        jQuery.each(self.projectModels, function (i, val) {
            if (val.type == self.selectedItemId()) {
                self.currentProjects(val.models);
                var projectsPerPage = parseInt($('#ProjectsPerPage').val(), "10");
                self.page(val.models.length / projectsPerPage);
                someProjectsLoaded = true;
            }
        });
        if (!someProjectsLoaded) {
            self.page(1);
            self.LoadMoreProjects();
        }
    };

    self.LoadMoreProjects = function() {
        var getProjectsUrl = $("#GetNextProjectsUrl").val();
        $.ajax({
            url: getProjectsUrl,
            data: {
                page: parseInt(self.page(), "10"),
                type: self.selectedItemId()
            },
            beforeSend: function () {
                //$("#ajaxload").show();
            },
            success: function (result) {
                var newlyAddedModelsArray = jQuery.map(result, function (val, i) {
                    var vm = new ProjectViewModel(val, self);
                    self.currentProjects.push(vm);
                    return vm;
                });
                self.page(parseInt(self.page(), "10") + 1);
                var typeAlreadyInChache = false;
                jQuery.each(self.projectModels, function (i, val) {
                    if(val.type == self.selectedItemId()) {
                        val.models = val.models.concat(newlyAddedModelsArray); //HERE IS WHERE I SUSPECT A COPY BE REFERENCE TAKES PLACE
                        typeAlreadyInChache = true;
                    }
                });
                if(!typeAlreadyInChache) {
                    self.projectModels.push({ type: self.selectedItemId(), models: newlyAddedModelsArray });
                }
            },
            error: function () {
                $("#error").show();
            }
        });
    };

In my KnockoutJS view model logic I have an observable array of currently displayed objects (project view models): self.currentProjects. Depending on an item filter setting (user chooses project types dynamically) self.currentProjects holds different objects that a UI is bound to.

At some point, where I need to load different projects into the view, I need to empty the currentProjects and put different set of models into it. I do this by calling self.currentProjects.removeAll(); The problem is when I call it, another observable array self.ProjectModels that should hold the reference to the same set of models also appears to be emptied up. How should I manage objects in an observable array, so that removal from one array doesn't cause the other to bee empty.

Here is a manipulation related code:

    //Another menu item is selected
    self.selectItem = function(newId) {
        self.selectedItemId(newId);
        self.currentProjects.removeAll(); //This is the point where self.ProjectModels also looses model references
        var someProjectsLoaded = false;
        jQuery.each(self.projectModels, function (i, val) {
            if (val.type == self.selectedItemId()) {
                self.currentProjects(val.models);
                var projectsPerPage = parseInt($('#ProjectsPerPage').val(), "10");
                self.page(val.models.length / projectsPerPage);
                someProjectsLoaded = true;
            }
        });
        if (!someProjectsLoaded) {
            self.page(1);
            self.LoadMoreProjects();
        }
    };

    self.LoadMoreProjects = function() {
        var getProjectsUrl = $("#GetNextProjectsUrl").val();
        $.ajax({
            url: getProjectsUrl,
            data: {
                page: parseInt(self.page(), "10"),
                type: self.selectedItemId()
            },
            beforeSend: function () {
                //$("#ajaxload").show();
            },
            success: function (result) {
                var newlyAddedModelsArray = jQuery.map(result, function (val, i) {
                    var vm = new ProjectViewModel(val, self);
                    self.currentProjects.push(vm);
                    return vm;
                });
                self.page(parseInt(self.page(), "10") + 1);
                var typeAlreadyInChache = false;
                jQuery.each(self.projectModels, function (i, val) {
                    if(val.type == self.selectedItemId()) {
                        val.models = val.models.concat(newlyAddedModelsArray); //HERE IS WHERE I SUSPECT A COPY BE REFERENCE TAKES PLACE
                        typeAlreadyInChache = true;
                    }
                });
                if(!typeAlreadyInChache) {
                    self.projectModels.push({ type: self.selectedItemId(), models: newlyAddedModelsArray });
                }
            },
            error: function () {
                $("#error").show();
            }
        });
    };
Share Improve this question edited Dec 3, 2012 at 18:03 xdumaine 10.3k7 gold badges64 silver badges107 bronze badges asked Dec 3, 2012 at 16:44 Maxim V. PavlovMaxim V. Pavlov 10.5k18 gold badges80 silver badges178 bronze badges 1
  • What does the rest of your view-model look like? – Markus Jarderot Commented Dec 3, 2012 at 17:04
Add a ment  | 

1 Answer 1

Reset to default 11

Calling removeAll empties the underlying array and your two observables appear to have a reference to the same array.

An easy choice is to set currentProjects to an empty array rather than calling removeAll.

self.currentProjects([]);

Now, the original array will remain as it was.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>