te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Referencing parent class in CoffeeScript from a jQuery callback - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Referencing parent class in CoffeeScript from a jQuery callback - Stack Overflow

programmeradmin2浏览0评论

I'm new to CoffeScript and I was wondering if there's a way of writing the following piece of code without referencing the global variable app:

class App 

    constructor: ->
        @ui = ui.init()
        $('#content-holder a[rel!=dialog]').live 'click', ->
            link = $(@).attr 'href'
            app.loadUrl link
            return false

    loadUrl: (href) ->
        # ...

app = new App()

Using the fat arrow doesn't work, as then I lose reference to the jQuery object, i.e.

class App   
    constructor: ->
        @ui = ui.init()
        $('#content-holder a[rel!=dialog]').live 'click', =>
            # @ now references App
            link = $(@).attr 'href'
            this.loadUrl link
            return false

    loadUrl: (href) ->
        # ...

The first piece of code works, but I want to get rid of the global variable if possible :-)

Cheers, Gaz.

I'm new to CoffeScript and I was wondering if there's a way of writing the following piece of code without referencing the global variable app:

class App 

    constructor: ->
        @ui = ui.init()
        $('#content-holder a[rel!=dialog]').live 'click', ->
            link = $(@).attr 'href'
            app.loadUrl link
            return false

    loadUrl: (href) ->
        # ...

app = new App()

Using the fat arrow doesn't work, as then I lose reference to the jQuery object, i.e.

class App   
    constructor: ->
        @ui = ui.init()
        $('#content-holder a[rel!=dialog]').live 'click', =>
            # @ now references App
            link = $(@).attr 'href'
            this.loadUrl link
            return false

    loadUrl: (href) ->
        # ...

The first piece of code works, but I want to get rid of the global variable if possible :-)

Cheers, Gaz.

Share Improve this question edited Oct 10, 2011 at 18:13 Gaz asked Oct 10, 2011 at 18:08 GazGaz 1,6482 gold badges18 silver badges22 bronze badges 4
  • Have you tried @loadUrl link instead of this.loadUrl If that doesn't work, please post the piled javascript. – Gazler Commented Oct 10, 2011 at 18:13
  • 1 @something is just a syntactic sugar for this.something, afaik – Guard Commented Oct 10, 2011 at 18:15
  • That won't work, because @ (this) will be referencing the anonymous callback function. – Gaz Commented Oct 10, 2011 at 18:16
  • @Gaz I was just copying from this jashkenas.github./coffee-script/#fat_arrow I read it as using the fat arrow binds the context to the value of this prior to entering the function. – Gazler Commented Oct 10, 2011 at 18:18
Add a ment  | 

2 Answers 2

Reset to default 11

Your click handler gets an event passed in... so you can get the best of both worlds with the "fat arrow" without the need to also reference self :

constructor: ->
    @ui = ui.init()
    $('#content-holder a[rel!=dialog]').live 'click', (e) =>
        link = $(e.target).attr 'href'
        @loadUrl link
        return false

Well, CS is just a higher-level syntax for JS.

In JS this can only reference a single object.

The fat arrow uses closure to make this equal to a higher level this, nothing more, and that's why it overrides this in a callback's scope

The plain arrow, in contrary, is just a function alias, and that's why this is a DOM element in the first case.

Finally, @something is trivially translated to this.something, and does nothing more.

So, my opinion - your best choice is really doing self = @ before the binding.

发布评论

评论列表(0)

  1. 暂无评论