what is the correct way to use coffeescript with e

2019-07-13 03:11发布

问题:

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.