EmberJS - 共享不同路由的控制器/模板 [英] EmberJS - sharing a controller / template for different routes

查看:76
本文介绍了EmberJS - 共享不同路由的控制器/模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的CRUD应用程序,允许创建新对象以及编辑它们。



用于添加记录和编辑记录的模板几乎相同。



他们使用完全相同的表单元素。
唯一的区别是表单下面的标题和按钮(应该更新或创建记录)



在我的实现中,我有



  • 2路线定义

  • 2路线对象

  • 2个控制器对象

  • 2个模板



我想知道如果




  • 如果所有这些对象都是必需的,我无法在这里重新使用




令我困扰的是:




  • 有两个单独的模板用于创建和编辑(虽然它们几乎相同)

  • 我有两个独立的控制器完全一样。



我希望在控制器级别上解决这个问题。
作为一个控制器装饰一个模型,在我的情况下,单个控制器对象可以包装一个新的记录或一个现有的记录。
它可以暴露一个属性(isNewObject),以便模板可以决定是否在新或编辑流。控制器可以有一个单一的createOrUpdate方法,它在更新方案中起作用。



路线



目前的实现是为我的资源使用新的和编辑路线。

  this.resource(locations,function(){
this.route(new,{path:/ new});
this.route(edit,{path:/:location_id});
});



新路线



new route 负责创建新的记录,当用户导航到新的记录屏幕时调用。

  App.LocationsNewRoute = Ember.Route.extend({
model:function(){
return App.Location.createRecord();
}
});



编辑路线



编辑路径负责编辑现有对象,当用户单击编辑按钮在概览屏幕中。
我没有扩展默认的编辑路径,而是使用自动生成的路径。



控制器



编辑控制器负责处理模板中发生的操作(保存或更新记录)



控制器唯一的事情是提交交易。



注意:我想这是一个重复使用的候选人,但是如何使用单个控制器来驱动2个不同的路线/模板?

  App.LocationsNewController = Ember.ObjectController.extend({
addItem:function(location){
location.transaction.commit();
this.get(target ).transitionTo(locations);
}
});

App.LocationsEditController = Ember.ObjectController.extend({
updateItem:function(location){
location.transaction.commit();
this.get target)。transitionTo(locations);
}
});



模板:



正如你所见,我在这里唯一的代码重用是部分(模型字段绑定)。
我还有2个控制器(新的和编辑)和2个模板。



新模板设置正确的标题/按钮,并重新使用表单partial。

 < script type =text / x-handlebarsdata-template-name =locations / new> 
< h1>新位置< / h1>
{{partiallocationForm}}
< p>< button {{action addItem this}}>添加记录< / button>< / p>
< / script>

编辑模板设置正确的标题/按钮,并重新使用局部形式。

 < script type =text / x-handlebarsdata-template-name =locations / edit> 
< h1>编辑位置< / h1>
{{partiallocationForm}}
< p>< button {{action updateItem this}}>更新记录< / button>< / p>
< / script>

部分

 code>< script type =text / x-handlebarsdata-template-name =_ locationForm> 
< form class =form-horizo​​ntal>
< div class =control-group>
< label class =control-labelfor =latitude>纬度< / label>
< div class =controls>
{{view Ember.TextField valueBinding =latitude}}
< / div>
< / div>
< div class =control-group>
< label class =control-labelfor =latitude>经度< / label>
< div class =controls>
{{view Ember.TextField valueBinding =longitude}}
< / div>
< / div>
< div class =control-group>
< label class =control-labelfor =accuracy>精度< / label>
< div class =controls>
{{view Ember.TextField valueBinding =accuracy}}
< / div>
< / div>
< / form>
< / script>

注意:我希望我可以在这里做一些高效/ 。



我想让我的模板看这个:(从控制器获取标题,并有一个单独的操作来处理更新和创建)

 < script type =text / x-handlebarsdata-template-name =locations> 
< h1> {{title}}< / h1>
{{partiallocationForm}}
< p>< button {{action createOrUpdateItem this}}>添加记录< / button>< / p>
< / script>



问题



我可以重新工作吗这个代码有更多的代码重用,或者尝试用单个模板和单个控制器为编辑记录和新记录流程做这个是不好的主意。
如果是这样,怎么办?我错过了我的2条路线(创建和编辑)将重新使用相同的控制器/模板的部分。

解决方案

你一直都很正确。



还可以在 edit route 中使用新的控制器和模板。 >

你只需要做两件事。



首先在中给出模板名称, renderTemplate 钩子编辑路线

  App.LocationsEditRoute = Ember.Route.extend({
setupController:function(controller,model){
this.controllerFor('locations.new')。setProperties({isNew:false,content:model} );
},
renderTemplate:function(){
this.render('locations / new')
}
});

由于新的模板被渲染,控制器也将是 newController ,您可以采取行动指向 newController 中的事件。



如果您要更改标题和按钮文本,您可以将它们作为计算属性观察 isNew 属性。



希望这个帮助。



PS:不要忘记在 setupController 中设置isNew属性为true $ c>新路线


I have a very simple CRUD application that allows for creating new objects as well as editing them.

The template used for adding a record and editing a record are almost identical.

They use the exact same form elements. The only difference is the title and the button below the form (that should either update or create a record)

In my implementation, I have

  • 2 route definitions
  • 2 route objects
  • 2 controller objects
  • 2 templates

I was wondering if

  • I can't promote re-use here
  • if all of these objects are required.

What bothers me :

  • I have 2 separate templates for create and edit (while they are almost identical)
  • I have 2 separate controllers that do exactly the same thing.

I was hoping to solve this on the controller level. As a controller decorates a model, in my case 1 single controller object could wrap either a new record or an existing record. It could then expose a property (isNewObject) so that the template can decide if we are in the "new" or "edit" flow. The controller could have a single createOrUpdate method that works both in the new and in the update scenario.

Routes

The current implementation is using a new and an edit route for my resource.

this.resource("locations", function(){
  this.route("new", {path:"/new"});
  this.route("edit", {path: "/:location_id" });
});

The new route

The new route is responsible for creating a new record and is called when the user navigates to the new record screen.

App.LocationsNewRoute = Ember.Route.extend({
  model: function() {
    return App.Location.createRecord();
  }
});

The edit route

The edit route is responsible for editing an existing object when the user clicks the edit button in the overview screen. I haven't extended the default edit route but instead I'm using the auto generated one.

Controllers

The new and edit controllers are responsible for handling the action that occurs in the template (either saving or updating a record)

The only thing both controllers do is commit the transaction.

Note: I guess this is a candidate for re-use, but how can I use a single controller for driving 2 different routes / templates ?

App.LocationsNewController = Ember.ObjectController.extend({
  addItem: function(location) {
    location.transaction.commit();
    this.get("target").transitionTo("locations");
  }
});

App.LocationsEditController = Ember.ObjectController.extend({
  updateItem: function(location) {
    location.transaction.commit();
    this.get("target").transitionTo("locations");
  }
});

Templates :

As you can see, the only code-reuse I have here is the partial (the model field binding). I still have 2 controllers (new and edit) and 2 templates.

The new templates sets the correct title / button and re-uses the form partial.

<script type="text/x-handlebars" data-template-name="locations/new" >
    <h1>New location</h1>
    {{partial "locationForm"}}
    <p><button {{action addItem this}}>Add record</button></p>
</script>

The edit templates sets the correct title / button and re-uses the form partial.

<script type="text/x-handlebars" data-template-name="locations/edit" >
    <h1>Edit location</h1>
    {{partial "locationForm"}}
    <p><button {{action updateItem this}}>Update record</button></p>
</script>

The partial

<script type="text/x-handlebars" data-template-name="_locationForm" >
  <form class="form-horizontal">
  <div class="control-group">
    <label class="control-label" for="latitude">Latitude</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="latitude"}}
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="latitude">Longitude</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="longitude"}}
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="accuracy">Accuracy</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="accuracy"}}
    </div>
  </div>
</form>
</script>

Note: I would expect that I can do something efficient/smarter here.

I would want my template to look this this : (getting the title from the controller, and have a single action that handles both the update and the create)

<script type="text/x-handlebars" data-template-name="locations" >
    <h1>{{title}}</h1>
    {{partial "locationForm"}}
    <p><button {{action createOrUpdateItem this}}>Add record</button></p>
</script>

Question

Can I re-work this code to have more code-reuse, or is it a bad idea to attempt to do this with a single template and a single controller for both the "edit record" and "new record" flows. If so, how can this be done ? I'm missing the part where my 2 routes (create and edit) would re-use the same controller / template.

解决方案

You were correct throughout.

And you can use the new controller and template in edit route also.

You have to do only two things.

First give the template name as new in the renderTemplate hook of edit route.

App.LocationsEditRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    this.controllerFor('locations.new').setProperties({isNew:false,content:model});
  },
  renderTemplate: function() {
    this.render('locations/new')         
  }
});

As the new template is rendered the controller also will be newController, and you can have the action to point to an event in the newController.

If you want to change the title and button text, you can have them as computed properties observing isNew property.

Hope this helps.

P.S: Don't forget to set the isNew property to true in the setupController of new route

这篇关于EmberJS - 共享不同路由的控制器/模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆