0

I have a three backbone routes

  routes: {
                   "foo": "foo",
                   "": "foo",
                   "foo/bar": "bar",
                   "foo/baz": "baz"

            },
            foo: function () {                                                      
                var fooView = new contentCollectionView({
                    collection: collection,
                    tagName: "div",
                    className: "foo"
                });

                fooView.close();
                FOO.content.show(fooView);

            },
          bar: function(){
            this.foo();
            ...
          },
          baz: function(){
            this.foo();
            ...
          }
    });  

With bar and baz functions I would really like to only run foo if fooView is not currently shown, otherwise the only thing they do is to change a css class that changes how foo is displayed.

user254694
  • 1,461
  • 2
  • 23
  • 46

1 Answers1

0

You can compare the url fragment,

bar: function () {
  if ( Backbone.history.getFragment() !== 'foo' ) {
    this.foo();
  } else {
    // do other stuff
  }
}

Let me know if this helps

Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
  • Hi, I guess I can use the getFragment to figure out if this.foo() has been called in our the history of my module - but if I don't call this.foo() currently then nothing gets rendered - I don't actually need to render anything in the case it has been rendered already, then I just need to switch a css class. I suppose I could use jquery or something to check for elements that would be there if things got rendered - but I would prefer a higher level more backbone like solution. – user254694 May 27 '14 at 14:24
  • Well, in that case, you need a manager to render views(probably an AppView or AppController). So whenever you create a new view, keep track of them in a property "current_view", then you can know what is the current_view & make decisions accordingly. See http://stackoverflow.com/questions/7379263/disposing-of-view-and-model-objects-in-backbone-js about the current_view implementation. – Mudassir Ali May 27 '14 at 14:33