CoffeeScript,玩!和问题

时间:2013-11-07 12:17:18

标签: javascript jquery playframework coffeescript

我一直在试验Play!框架已经持续了几个星期,并且已经完成了Zen任务示例应用程序的一半,并且chapter 5 to 6中的信息似乎存在很大差距。

我已经按照描述创建了main.coffee文件,并且应用程序编译正常,但是我收到以下错误“ Uncaught TypeError:对象#<HTMLUListElement>的属性'children'不是函数< / EM>”。

这意味着我正在尝试在HTML元素上调用jQuery,但是我从字面上复制并粘贴了样本,所以无法理解我能做什么,它必须适用于编写教程的人吗? !

无论如何,我正在使用Play 2.2.0,而coffeescript的编写如下......

class Drawer extends Backbone.View

$ -> 
   drawer = new Drawer el: $("#travelgroups")

class Drawer extends Backbone.View
initialize: ->
    @el.children("li").each (i,group) ->
        new Group
            el: $(group)
        $("li",group).each (i,travelgroup) ->
            new TravelGroup
                el: $(travelgroup)

显示错误的生成的main.js文件的部分位于

之下
    Drawer.prototype.initialize = function() {
  return this.el.children("li").each(function(i, group) {
    new Group({
      el: $(group)
    });
    return $("li", group).each(function(i, travelgroup) {
      return new TravelGroup({
        el: $(travelgroup)
      });
    });
  });
};

作为次要问题,咖啡文件本身是否应包含“内部”类声明,还是应该单独创建.java类文件?就像我说的那样,教程章节之间的详细解释存在很大的差距所以有点困惑,因为coffescript对我来说是全新的,并且之前从未见过实际的.java类文件的类声明?

编辑...

完整的咖啡脚本文件如下。压痕已经是原样了。

$(".options dt, .users dt").live "click", (e) ->
    e.preventDefault()
    if $(e.target).parent().hasClass("opened")
        $(e.target).parent().removeClass("opened")
    else
        $(e.target).parent().addClass("opened")
        $(document).one "click", ->
            $(e.target).parent().removeClass("opened")
    false

$.fn.editInPlace = (method, options...) ->
    this.each ->
        methods = 
            # public methods
            init: (options) ->
                valid = (e) =>
                    newValue = @input.val()
                    options.onChange.call(options.context, newValue)
                cancel = (e) =>
                    @el.show()
                    @input.hide()
                @el = $(this).dblclick(methods.edit)
                @input = $("<input type='text' />")
                    .insertBefore(@el)
                    .keyup (e) ->
                        switch(e.keyCode)
                            # Enter key
                            when 13 then $(this).blur()
                            # Escape key
                            when 27 then cancel(e)
                    .blur(valid)
                    .hide()
            edit: ->
                @input
                    .val(@el.text())
                    .show()
                    .focus()
                    .select()
                @el.hide()
            close: (newName) ->
                @el.text(newName).show()
                @input.hide()
        # jQuery approach: http://docs.jquery.com/Plugins/Authoring
        if ( methods[method] )
            return methods[ method ].apply(this, options)
        else if (typeof method == 'object')
            return methods.init.call(this, method)
        else
            $.error("Method " + method + " does not exist.")


class Drawer extends Backbone.View

$ -> 
     drawer = new Drawer el: $("#travelgroups")

class Drawer extends Backbone.View
    initialize: ->
        @el.children("li").each (i,group) ->
            new Group
                el: $(group)
            $("li",group).each (i,travelgroup) ->
                new TravelGroup
                    el: $(travelgroup)

class Group extends Backbone.View
    events:
        "click    .toggle"          : "toggle"
        "click    .newTravelGroup"  : "newTravelgroup"
    toggle: (e) ->
        e.preventDefault()
        @el.toggleClass("closed")
        false
    newProject: (e) ->
        @el.removeClass("closed")
        r = jsRoutes.controllers.TravelGroups.add()
        $.ajax
            url: r.url
            type: r.type
            context: this
            data:
                group: @el.attr("data-group")
            success: (tpl) ->
                _list = $("ul",@el)
                _view = new TravelGroup
                    el: $(tpl).appendTo(_list)
                _view.el.find(".name").editInPlace("edit")
            error: (err) ->
                $.error("Error: " + err)

class TravelGroup extends Backbone.View

AND HTML ASOWOW ... group.scala.html

@(group: String, travelgroups: List[TravelGroup])

<li data-group="@group">
    <span class="toggle"></span>
    <h4 class="groupName">@group</h4>
    <span class="loader">Loading</span>
    <dl class="options">
        <dt>Options</dt>
        <dd>
            <button class="newTravelGroup">New travel group</button>
            <button class="deleteGroup">Remove group</button>
        </dd>
    </dl>
    <ul>
        @travelgroups.map { travelgroup =>
            @views.html.travelgroups.item(travelgroup)
        }
    </ul>
</li>

... item.scala.html

@(travelgroup: TravelGroup)

<li data-project="@travelgroup.id">
    <a class="name" href="#">@travelgroup.name</a>
    <button class="delete" href="#">Delete</button>
    <span class="loader">Loading</span>
</li>

0 个答案:

没有答案