我的兩個問題在加粗,但有相當數量的代碼是給出的上下文。這些問題主要涉及到router.js何時被命中以及如何ember知道要加載哪些模板。Ember.js當我們點擊一個link_to時,我們是否先點擊router.js?或者是相關的模板加載?我們什麼時候不打router.js?
我正在製作一個玩具庫搜索應用程序。我對如何連接路由器,路由處理程序,模板和控制器有一些疑問。
這是我的路由器:
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('about');
this.route('contact');
this.route('admin', function() {
this.route('invitations');
this.route('contacts');
});
this.route('libraries', function() {
this.route('new');
this.route('edit', { path: '/:library_id/edit' });
});
});
export default Router;
所以,當我訪問/庫...
- 我打router.js文件FIRST
的router.js文件首先將我帶到libraries.hbs模板的右側?這是模板:
<h1>Libraries</h1> <div class="well"> <ul class="nav nav-pills"> {{#link-to 'libraries.index' tagName="li"}}<a href>List all them</a>{{/link-to}} {{#link-to 'libraries.new' tagName="li"}}<a href>Add new</a>{{/link-to}} </ul> </div> {{outlet}}
出口呈現庫/ index.hbs模板,然後對不對?這是我的庫/ index.hbs:
<h2>List</h2>
<div class="row">
{{#each model as |library|}}
<div class="col-md-4">
<div class="panel panel-default library-item">
<div class="panel-heading">
<h3 class="panel-title">{{library.name}}</h3>
</div>
<div class="panel-body">
<p>Address: {{library.address}}</p>
<p>Phone: {{library.phone}}</p>
</div>
<div class="panel-footer text-right">
{{#link-to 'libraries.edit' library.id class='btn btn-success btn-xs'}}Edit{{/link-to}}
<button class="btn btn-danger btn-xs" {{action 'deleteLibrary' library}}>Delete</button>
</div>
</div>
</div>
{{/each}}
</div>
當用戶點擊此鏈接:
{{#link-to 'libraries.edit' library.id class='btn btn-success btn-xs'}}Edit{{/link-to}}
我們在哪裏先打?我們是否再次點擊router.js? router.js中的編輯路徑有一個路徑,那是幹什麼的? URL如何呈現? library_id來自哪裏?
這是我的編輯模板:
<h2>Edit Library</h2>
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
{{input type="text" value=model.name class="form-control" placeholder="The name of the Library"}}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Address</label>
<div class="col-sm-10">
{{input type="text" value=model.address class="form-control" placeholder="The address of the Library"}}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
{{input type="text" value=model.phone class="form-control" placeholder="The phone number of the Library"}}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" {{action 'saveLibrary' model}}>Save changes</button>
</div>
</div>
</div>
提交按鈕有一個名爲「saveLibrary」一個行動,這需要一個對象。 當我點擊提交按鈕時,我不會再次點擊router.js文件了嗎?所發生的只是它尋找在當前上下文中定義的動作,這是路由處理程序的權利?
這裏是我的路線/庫/ edit.js文件:
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('library', params.library_id);
},
actions: {
saveLibrary(newLibrary) {
newLibrary.save().then(() => this.transitionTo('libraries'));
},
willTransition(transition) {
let model = this.controller.get('model');
if (model.get('hasDirtyAttributes')) {
let confirmation = confirm("Your changes haven't saved yet. Would you like to leave this form?");
if (confirmation) {
model.rollbackAttributes();
} else {
transition.abort();
}
}
}
}
});
而且saveLibrary方法有一個過渡,然後打router.js文件嗎?轉換會根據路由器中如何定義它們的方式更改url.js文件?