0

i have this code

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);

the path variable is the return of this function

 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;
    },

My problem is that this "path" variable is acually a breadcrumb array, and every time i modify the "path" variable, its acually modifing the global Server.adminPages variable, i tried couple of thinks to clone this object like:

  1. [].concat(Server.adminPages[index])
  2. $.extend({}, Server.adminPages[index])
  3. Server.adminPages[index].splice(0)
  4. jquery $.each()

None of the above worked its just keep to change the global variable if its matter, the adminPages code is hard Coded in the html header

some background.. the path array contain some mustache variables like {{action}} , to determine when the page is in edit state or create state. the problem is that if i will navigate to "Create" action, the mustache will be parsed to "Create" , than if i will navigate to edit since the "path" variable was already changed ( or w/e the js was doing there ) i will still see "Create" because the {{action}} string was already parsed

roy
  • 585
  • 5
  • 14
  • If an item in an _Array Object_ is another _Object_, when you create a _clone_ of just the top level, the item is still pointing to the same _Object_ as in the original _Array_. It also looks like you're trying to use Array methods on a generic (non-array) Object; this won't work. – Paul S. Feb 25 '14 at 21:42
  • 1
    Take a look to this: http://stackoverflow.com/q/728360/462889 – Allende Feb 25 '14 at 22:16

0 Answers0