Backbone.Marionette Fade Transition for only specific regions?
Asked Answered
B

2

9

I know I can override all regions to add a fade transition by using the following.

Marionette.Region.prototype.open = function(view){
  this.$el.hide();
  this.$el.html(view.el);
  this.$el.fadeIn()
}

Is there a way to only override specific regions or views? I have certain regions in my layout that I would like to be able to fade in while other regions should be rendered instantly.

Thanks,

dk

Brunabrunch answered 9/8, 2012 at 19:11 Comment(0)
P
16

You can define a custom Region the way you can define any Backbone object, and add this code to that region type.


MyRegion = Backbone.Marionette.Region.extend({

  el: "#some-element",

  open: function(view){
    this.$el.hide();
    this.$el.html(view.el);
    this.$el.fadeIn();
  }

});

MyApp.addRegions({
  myRegion: MyRegion
});

Note that I included the el in the region definition. If you want to re-use this across multiple regions, you'll have to create a base region and extend from that for each one that you need.


FadeInRegion = Backbone.Marionette.Region.extend({

  open: function(view){
    this.$el.hide();
    this.$el.html(view.el);
    this.$el.fadeIn();
  }

});

MyApp.addRegions({
  myRegion: FadeInRegion.extend({el: "#some-element"}),
  anotherRegion: FadeInRegion.extend({el: "#another-element"})
});
Prosecutor answered 9/8, 2012 at 21:14 Comment(4)
Thanks for the tip Derick. I was not able to get code snippet to work as is, but I added... Backbone.Marionette.Region.prototype.open.call(this, view, appendMethod); ... to bottom of FadeInRegion open method and it was all good.Heinz
For those trying to get the second code snippet to work, try changing the second snippet to: var FadeInRegion = Backbone.Marionette.Region.extend({Devlin
Will this allow for the two views to co-exist for the duration of the animation? Say for eg. we have a pageContainer region showing a view ViewOne and we want to bring in ViewTwo in the same region with a slideDown animation, will we see the content getting overlapped gradually by the new view ViewTwo?Stockmon
attachHtml method has to be overridden instead of open refMunford
D
-1

Another option that I just used was to override the open method for animations was to create a separate config file, override the open method in that config file, and conditional logic to test for className. So here's what I did with coffee script and using Marionette modules.

Create my view:

@Item.module "ItemApp.Add", (Add, App, Backbone, Marionette, $,_) ->

    class Add.Item extends Marionette.ItemView

        template: "#add-item"
        className: "add-modal"

And in my config file I just test the className to perform the desired animation:

do (Marionette) ->
    _.extend Marionette.Region::,
        open: (view) ->
            if view.el.className == "add-modal"
                console.log "the add-modal has been called"
                @$el.hide()
                @$el.html(view.el)
                @$el.show().animate "left": '0', queue: false
Delp answered 19/6, 2013 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.