what is the correct way to use coffeescript with ember´s Mixins?

只愿长相守 提交于 2019-12-12 17:01:10

问题


Apologies for my english,

I am trying to extend a View with a mixin. The code is coffeescript :

View - films_film_view.js.coffee

App.FilmsFilmView = Em.View.extend App.ModalViewMixin,

  templateName: 'films/show'

Mixin - modal_view_mixin.js.coffee

App.ModalViewMixin = Em.Mixin.create

  modalView: null

  click: ->
    @showModalView()

  close: ->
    @closeModalView()


  closeModalView: ->
    if @modalView
      @modalView.close()


  showModalView: ->
    @closeModalView()
    @modalView = @createModalView()
    if @modalView
      @modalView.append()

when i try to load the app, the app throws this error:

Assertion failed: Expected hash or Mixin instance, got [object Undefined] 

so, what is the correct way to write this ?

Thanks in advance

Update:

Solved with :

App.ModalViewMixin = Em.Mixin.create

  modalView: null

  click: ->
    @showModalView()

  close: ->
    @closeModalView()


  closeModalView: ->
    if @modalView
      @modalView.close()


  showModalView: ->
    @closeModalView()
    @set modalView, @createModalView()
    if @modalView
      @modalView.append()



App.FilmsFilmView = Em.View.extend App.ModalViewMixin,

  templateName: "films/show"

Looks like when the app loads "FilmsFilmView", the mixin still is not initialized and it causes the error.


回答1:


This happen because when running:

App.FilmsFilmView = Em.View.extend(App.ModalViewMixin, ...

App.ModalViewMixin is undefined, it have to be defined before of it usage.

Ensure that App.ModalViewMixin appears before than App.FilmsFilmView.



来源:https://stackoverflow.com/questions/18157171/what-is-the-correct-way-to-use-coffeescript-with-ember%c2%b4s-mixins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!