javascript Ember的路线生命周期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Ember的路线生命周期相关的知识,希望对你有一定的参考价值。

{
  "version": "0.12.1",
  "EmberENV": {
    "FEATURES": {}
  },
  "options": {
    "use_pods": false,
    "enable-testing": false
  },
  "dependencies": {
    "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js",
    "ember": "2.18.0",
    "ember-template-compiler": "2.18.0",
    "ember-testing": "2.18.0"
  }
}
<h1>{{appName}}</h1>


<nav>
  {{#link-to 'index'}}Index{{/link-to}} |
  {{#link-to 'about' (query-params state=null)}}About{{/link-to}} |
  {{#link-to 'about' (query-params state="hello")}}About With Queryparams{{/link-to}} |
  {{#link-to 'some.nested.route'}}Some/Nested/Route{{/link-to}}
</nav>
<br>
<hr>
<div class="legend legend--validation">Validation Phase (paused if we use promises)</div>
<div class="legend legend--setup">Setup Phase (NOT if we use promises)</div>
<div class="legend legend--events">Events ( Wrap route transition)</div>
<hr>

<br>
<button {{action "clearLog"}}>Clear Logs</button>
<br><br>

{{outlet}}
body {
  margin: 12px 16px;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.log-item {
  display: block;
  font-size:16px;
}
.log-setup-item{
  color: purple;
}
.log-validation-item{
  color: red;
}

.log-action-item {
  color: green;
}

.legend{
  font-size:12px;
  padding-left:10px;
  margin-bottom:3px;
}

.legend--validation{
  border-left:15px solid red;
}
.legend--setup{
  border-left:15px solid purple;
}
.legend--events{
  border-left:15px solid green;
}
import Ember from 'ember';
import LogRoute from '../../log-route';

export default LogRoute.extend();
import Ember from 'ember';
import LogRoute from '../log-route';

export default LogRoute.extend();
import Ember from 'ember';
import LogRoute from './log-route';

export default LogRoute.extend();
import Ember from 'ember';

function logValidationHook(property) {
  return function() {
    let log = `<span class="log-item log-validation-item log-hook-item">${this._debugContainerKey} - ${property}</span>`;
    Ember.$('body').append(log);
    return this._super(...arguments);
  };
}

function logSetupHook(property) {
  return function() {
    let log = `<span class="log-item log-hook-item log-setup-item">${this._debugContainerKey} - ${property}</span>`;
    Ember.$('body').append(log);
    return this._super(...arguments);
  };
}

function logAction(property) {
  return function() {
    let log = `<span class="log-item log-action-item">${this._debugContainerKey} - ${property}</span>`;
    Ember.$('body').append(log);
    return this._super(...arguments);
  };
}

export default Ember.Route.extend({
  activate: logSetupHook('activate'),
  deactivate: logSetupHook('deactivate'),
  model: logValidationHook('model'),
  afterModel: logValidationHook('afterModel'),
  redirect: logValidationHook('redirect'),
  beforeModel: logValidationHook('beforeModel'),
  setupController: logSetupHook('setupController'),
  resetController: logSetupHook('resetController'),
  renderTemplate: logSetupHook('renderTemplate'),
  
  actions: {
    willTransition: logAction('will transition (action)'),
    didTransition: logAction('did transition (action)')
  }
});
import Ember from 'ember';
import LogRoute from './log-route';

export default LogRoute.extend();
import Ember from 'ember';
import LogRoute from './log-route';

export default LogRoute.extend();
import Ember from 'ember';
import LogRoute from './log-route';

export default LogRoute.extend({
	queryParams: {
    state: {
      refreshModel: true
    }
  }
});
import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: 'none',
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('about');
  
  this.route('some', function() {
    this.route('nested', function() {
      this.route('route');  
    });
  });
});

export default Router;
import Ember from 'ember';

export default Ember.Controller.extend({
  appName: `Ember's Route Lifecycle`,
  
  actions: {
    clearLog() {
      Ember.$('.log-item').remove();
    }
  }
});
import Ember from 'ember';

export default Ember.Controller.extend({
  queryParams:["state"],
  state: null
});

以上是关于javascript Ember的路线生命周期的主要内容,如果未能解决你的问题,请参考以下文章

Android学习路线Activity生命周期——启动一个Activity

Ember:模型更改时重新渲染路线的模板

在 Ember 中获取当前路线名称

Ember对测试路线钩子和动作感到困惑

Android学习的三个终极问题及学习路线规划,真香定律

每个 Android 版本的生命周期/支持期