如何在emberjs渲染方法中传递参数?

时间:2015-02-06 16:36:57

标签: javascript twitter-bootstrap ember.js modal-dialog

我试图在渲染时将参数传递给模态视图。这是我到目前为止所得到的:

在我的申请路线中,我有:

export default Ember.Route.extend({
  actions: {
    openModal: function(modal, opts) {

      return this.render(modal, {
        into: 'application',
        outlet: 'modal'
        model: function(){ return opts },
        controller: modal
      });
    },
    closeModal: function() {
      return this.disconnectOutlet({
        outlet: 'modal',
        parentView: 'application'
      });
    }
  }
});

我有一个通用的模态视图:

import Ember from 'ember';

export default Ember.View.extend({
  tagName: 'div',
  classNames: 'modal'.w(),
  didInsertElement: function() {
    console.log("did insert element");
    this.$().attr('id', 'modal');
    this.$().modal({
      keyboard: false,
      backdrop: 'static'
    });

    return this.$().modal('show');
  },
  willDestroyElement: function() {
    return this.$().modal('hide');
  }
});

以及特定于我的模态的视图:

import ModalView from '../modal';
export default ModalView.extend();

我希望'opts'(在openModal中作为参数传递)进行处理,以便我可以根据它填充模态的内容。到目前为止,我的模态控制器无法得到任何东西:

import ModalController from '../modal'

export default ModalController.extend({
  init: function(){
    console.log('initializing modal');
    console.log(this.get('model'));
    console.log(this);
  },
  actions: {
    confirm: function() {
      //alert('OK, it will be done!');
      return this.send('closeModal');
    }
  }
});

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

所以我想出来了,必须做得更好,但以下内容对我有用:

申请途径:

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    openModal: function(modal, opts) {
      this.controllerFor(modal).set('model', opts);
      return this.render(modal, {
        into: 'application',
        outlet: 'modal'
      });
    },
    closeModal: function() {
      return this.disconnectOutlet({
        outlet: 'modal',
        parentView: 'application'
      });
    }
  }
});

我的模态窗口的控制器:

import ModalController from '../modal'

export default ModalController.extend({
  modalContent: "test",
  actions: {
    confirm: function() {
      //alert('OK, it will be done!');
      return this.send('closeModal');
    }
  },
  idUpdated: function(){
    var id = this.get('model').id;
    console.log(id);
    // Update modal content with the id here
    // Ajax request goes here.

  }.observes("id")
});

答案 1 :(得分:0)

尝试更改为:

openModal: function(modal, opts) {

  return this.render(modal, {
    into: 'application',
    outlet: 'modal'
    model: opts, // here
    controller: modal
  });
}