[英]Create Close Event To Emit from Custom Overlay
So I've got a rails app using backbone.js and coffeescript. 所以我有一个使用ribs.js和coffeescript的Rails应用程序。 I've created a backbone view called "Overlay" that looks like this:
我创建了一个名为“ Overlay”的主视图,如下所示:
class MyApp.Views.Overlay extends Backbone.View
template: JST['overlay']
contentDiv: ->
$('.overlay-content')
initialize: (options) ->
@content = options.content
@width = options.width
render: ->
@$el.append @template()
@contentDiv().append @content
events: ->
'click': 'overlayClick'
'click .close': 'removeOverlay'
overlayClick: (event) ->
target = $(event.target)
unless target.hasClass('overlay-content') or target.parents('.overlay-content').length > 0
@removeOverlay()
removeOverlay: ->
@$('.overlay').remove()
and my template (which is hamljs) 和我的模板(是hamljs)
.overlay
.overlay-content
%a.close Close
and I call it like so 我这样称呼它
@overlay = new MyApp.Views.Overlay(el: 'body', content: 'some content)
@overlay.render()
This is real simple, essentially you pass it some arbitrary string or html to be rendered in the overlay and it adds a nice lil overlay to the body. 这实际上很简单,实际上,您可以向其传递一些任意的字符串或html以在覆盖中呈现,并且它会向主体添加漂亮的lil覆盖。
What I am trying to do is write some code in the view that handles the generation of this overlay like so: 我想做的是在视图中编写一些代码来处理此叠加层的生成,如下所示:
@overlay.on('close', someFunction)
However, I haven't the faintest clue how to go about doing this. 但是,我不知道如何执行此操作。 Any suggestions?
有什么建议么?
You can use backbone to trigger events from the view 您可以使用主干从视图触发事件
removeOverlay: ->
@$('.overlay').remove()
@trigger('close')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.