javascript - coffeescript class - calling calling one class method from an other -
i have rewritten embedded js in rails app coffeescript..and new coffeescript...
my approach mix of these articles:
my code:
s = undefined class app.photolike settings: modalelement: $('#mymodal') likebuttonid: '#like' photo_id: $('.image_info').attr('photo_id') numberoflikes: $('#likes_num') constructor: (@el) -> console.log('constructor called') s = @settings @binduiactions() return binduiactions: -> console.log('binduiactions called') $('#mymodal').on 'click', '#like', -> likephoto() $(document).on "page:change", -> pagechange() pagechange: -> console.log('pagechange called') photo = new app.photo likephoto: -> console.log('likephoto called') url = '/photos/' + s.photo_id + '/like' $.get url, (data) -> s.numberoflikes.html data['likes'] + ' likes' $(s.likebuttonid).toggleclass 'btn-success'
this part of model gets loded ajax , once call succeeds create above class (instance of):
new (app.photolike)
my problem. modal gets loaded seems good. when trigger event:
$('#mymodal').on 'click', '#like', -> likephoto()
i get:
photo.like.self-942fcd8….js?body=1:25 uncaught referenceerror: likephoto not defined
so, not know likephoto function. have tried calling this.likephoto , @likephoto, refers button element triggers event.
so how do this? other approaches welcome. not sure need classes...but want structured code...
likephoto() function of our object/class app.photolike
. , you're calling local function.
you can instead
binduiactions: -> _this = #this change inside, have have reference our object here console.log('binduiactions called') $('#mymodal').on 'click', '#like', -> _this.likephoto() ...
let me know if works.
Comments
Post a Comment