Javascript拒绝克隆数组

时间:2014-02-25 21:39:58

标签: javascript jquery backbone.js underscore.js marionette

我有这个代码

var path = App.request('app:page:breadcrumb', index);
        if(parseVals) {
            path[ path.length - 1 ].title = this._parseVals(path[ path.length - 1 ].title, parseVals);
        }

        this.BreadCrumbView.collection.reset(path);

路径变量是此函数的返回值

 getBreadcrumbs: function(index, getCollection){

        // return empty collections of index set to null and get collection flagged to true
        if(! index && getCollection){
            return new Breadcrumbs.Collection({});
        }
        // Generate breadcrumbs object
        var breadcrumbArr = Server.adminPages[index]['breadcrumb_path'].split('/'),
            breadcrumbs   = _.values(array_intersect_key(Server.adminPages ,array_flip(breadcrumbArr)));

        // Set selected breadcrumb
        $.each(breadcrumbs, function(index){
            breadcrumbs[index]['is_active'] = index == breadcrumbs.length - 1;
        });

        // Return breadcrumbs as collection or object
        return getCollection
            ? new Breadcrumbs.Collection(breadcrumbs)
            : breadcrumbs;
    },

我的问题是这个“路径”变量实际上是一个痕迹数组,每次我修改“path”变量时,它都会修改全局的Server.adminPages变量, 我尝试了几个想克隆这个对象,如:

  1. []。CONCAT(Server.adminPages [指数])
  2. $ .extend({},Server.adminPages [index])
  3. Server.adminPages [指数] .splice(0)
  4. jquery $ .each()
  5. 以上都没有工作,只是保持改变全局变量 如果它的问题,adminPages代码在html标题中是硬编码的

    一些背景.. path数组包含一些胡须变量,如{{action}},用于确定页面何时处于编辑状态或创建状态。 问题是如果我将导航到“创建”动作,小胡子将被解析为“创建”,而不是我将导航到编辑,因为“路径”变量已经改变(或者没有js在那里做)我仍然会看到“创建”因为已经解析了{{action}}字符串

0 个答案:

没有答案
相关问题