简体   繁体   English

ember-cli:使用查找创建的视图绑定模板操作

[英]ember-cli: bind template action from view created using lookup

Ember : 1.10.0 灰烬:1.10.0

Ember Data : 1.0.0-beta.16 灰烬数据:1.0.0-beta.16

jQuery : 1.11.2 的jQuery:1.11.2

I'm using this.container.lookup in order to create a view dynamically in my controller like this: 我使用this.container.lookup以便在控制器中动态创建视图,如下所示:

var popup = this.container.lookup('view:map-popup', {singleton: false});
var ctrl = this.container.lookup('controller:map-popup', {singleton: false});
popup.set('controller', ctrl);
popup.createElement();

the view is defined like this: 视图的定义如下:

export default Ember.View.extend({
  templateName: "mapPopup",
  classNames: ["map-popup"]
});

The view template: 视图模板:

<a class="popup-closer" {{action 'closePopup'}}></a>
<div class="popup-content">{{content}}</div>

and the view's controller: 和视图的控制器:

export default Ember.ObjectController.extend({

  hide: function() {
    console.log("i'm hidden");
  },

  actions: {
    closePopup: function() {
      this.hide();
    }
  }
});

The view is inserted correctly in the DOM by my controller: 我的控制器已将视图正确插入DOM中:

<div class="ember-view map-popup" id="ember643">
  <a data-ember-action="645" class="popup-closer"></a>
  <div class="popup-content">571187.3674937992,5721182.413928764</div>
</div>

But nothing happens when I click on the popup-closer. 但是,当我单击弹出窗口时,什么也没有发生。

I would be glad if someone could show me how to bind the action to the view's controller. 如果有人可以向我展示如何将操作绑定到视图的控制器,我将感到非常高兴。

Following albert advice, I tried to setup a component: 遵循阿尔伯特的建议,我尝试设置一个组件:

//app/components/map-popup.js
export default Ember.Component.extend({
  classNames: ['map-popup'],
  layoutName: 'components/map-popup',

  content: function() {
    return "some content";
  },

  hide: function() {
    console.log('hidden');
  },

  actions: {
    closePopup: function() {
      this.hide();
    }
  }
});

template is the same. 模板是相同的。 as before, but inthe templates/components folder. 和以前一样,但是在template / components文件夹中。

The result is worst than before. 结果比以前更糟。 I get the popup to display the function as text instead of the content text... 我得到了将功能显示为文本而不是内容文本的弹出窗口...

在此处输入图片说明

The action is now triggered! 现在已触发该动作! That's great, the problem with the content not being displayed has bene solved using jquery: 太好了,使用jquery可以很好地解决内容不显示的问题:

  setUp: function() {
    this.$('.popup-content').html(this.content());
  }.on('didInsertElement'),

I think it's ugly, I would much rather have a one way binding the the {{content}} in handlebar... But I cannot figure out how to do this. 我认为这很丑陋,我宁愿有一种将{{content}}绑定到车把的方法……但是我不知道该怎么做。

@albertjan is right. @albertjan是正确的。 This has to be a component. 这必须是一个组成部分。 Thats the right direction. 那是正确的方向。

Now for the issue that content() is not returning values can be solved if you make a computed property for it like this 现在,对于这样的问题,如果您像这样为它创建一个计算属性 ,就可以解决content()不返回值的问题

export default Ember.Component.extend({
  classNames: ['map-popup'],
  layoutName: 'components/map-popup',

  /*Notice the .property('') */
  content: function() {
    return "some content";
  }.property(''),

  hide: function() {
    console.log('hidden');
  },

  actions: {
    closePopup: function() {
      this.hide();
    }
  }
});

Why this works, When you use a simple function in template its nothing more than a attribute having some string value (which is function() body) 为什么这样做有效,当您在模板中使用简单函数时,它只不过是具有一些字符串值的属性(它是function()主体)

To use the function's return value Ember use concept of computed properties. 要使用函数的返回值Ember,请使用计算属性的概念。

Computed properties also accept arguments and are useful for combining results of two or more properties like this. 计算属性也接受自变量,对于合并两个或多个这样的属性的结果很有用。 Its output updates when any dependent properties changes. 任何相关属性更改时,其输出都会更新。

Example can be found here on Emberjs.com 可以在Emberjs.com上找到示例

var Person = Ember.Object.extend({
  // these will be supplied by `create`
  firstName: null,
  lastName: null,

  fullName: function() {
    var firstName = this.get('firstName');
    var lastName = this.get('lastName');

   return firstName + ' ' + lastName;
  }.property('firstName', 'lastName')
});

var tom = Person.create({
  firstName: 'Tom',
  lastName: 'Dale'
});

tom.get('fullName') // 'Tom Dale'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM