barbajs源码分析

2017-12-19  本文已影响95人  这是我用来记录技术的一个博客

就算有官方文档 其实做起来还是有些一头雾水啊。。。。
官方地址:http://barbajs.org/index.html

运行流程如下:
每一个应用基本上都先调用这两个方法

Barba.Pjax.init();
Barba.Prefetch.init();

BaseView类分析

BaseView里面写了一些接口函数和一些监听函数,如果trigger了相关监听,则调用里面的接口函数。

用法如下:


var Home = Barba.BaseView.extend({
  namespace: 'home',
  onEnter: function() {
  },
  onEnterCompleted: function() {
    document.body.classList.add('home');
  },
  onLeave: function() {
    document.body.classList.remove('home');
  },
  onLeaveCompleted: function() {
  }
});

Home.init();

var About = Barba.BaseView.extend({
  namespace: 'about',
  onEnter: function() {
  },
  onEnterCompleted: function() {
    document.body.classList.add('about');
  },
  onLeave: function() {
    document.body.classList.remove('about');
  },
  onLeaveCompleted: function() {
  }
});

About.init();

Barba.Pjax.start();

在pjax里面用到了上述监听
init方法

 Dispatcher.trigger('initStateChange', this.History.currentStatus());
    Dispatcher.trigger('newPageReady',
      this.History.currentStatus(),
      {},l
      container,
      this.Dom.currentHTML
    );
    Dispatcher.trigger('transitionCompleted', this.History.currentStatus());

onStateChange方法

Dispatcher.trigger('initStateChange',
      this.History.currentStatus(),
      this.History.prevStatus()
    );
var transitionInstance = transition.init(
      this.Dom.getContainer(),
      newContainer
    );

    newContainer.then(
      this.onNewContainerLoaded.bind(this)
    );

    transitionInstance.then(
      this.onTransitionEnd.bind(this)
    );
/**
   * new container一准备好就调用
   *
   * @memberOf Barba.Pjax
   * @private
   * @param {HTMLElement} container
   */
  onNewContainerLoaded: function(container) {
    var currentStatus = this.History.currentStatus();
    currentStatus.namespace = this.Dom.getNamespace(container);

    Dispatcher.trigger('newPageReady',
      this.History.currentStatus(),
      this.History.prevStatus(),
      container,
      this.Dom.currentHTML
    );
  },

  /**
   * 动画一结束就调用
   *
   * @memberOf Barba.Pjax
   * @private
   */
  onTransitionEnd: function() {
    this.transitionProgress = false;

    Dispatcher.trigger('transitionCompleted',
      this.History.currentStatus(),
      this.History.prevStatus()
    );
  }
上一篇 下一篇

猜你喜欢

热点阅读