js 模块化代码流程

来源:互联网 发布:软件项目实施费用 编辑:程序博客网 时间:2024/06/05 00:42

varModule = function(){

  this.init();

};

 

// 初始化

Module.prototype.init = function(){

  this.fetchData(function(){

    // do something

  });

};

 

// 绑定事件

Module.prototype.bindEvent = function(){

  // ...

};

 

// 获取数据

Module.prototype.fetchData = function(cb){

  varself = this;

  ajax({}).then(function(data){

    self.renderData(data);

  }).catch(function(){

    self._fetchDataFailed();

  }).fin(function(){

    cb && cb();

  });

};

 

// 渲染数据

Module.prototype.renderData = function(data){

  data = this._resolveData(data);

  // ...

  this.bindEvent();

};

 

// 处理数据

Module.prototype._resolveData = function(){

  // ...

};

 

// 加载失败

Module.prototype._fetchDataFailed = function(){

  // ...

};

1 0