Backbone.Marionette Fade Transition for only specific regions?

冷暖自知 提交于 2019-12-02 21:06:00
Derick Bailey

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

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!