javascript - Access class instance from a function passed into a React component and triggered there -
i have backbone/react app. i'm trying keep backbone logic in backbone view (foodlist in example), can pass methods components, have components trigger them, , run necessary logic within view (i.e. manipulate backbone collection), keeping backbone's business out of react's business.
the trouble i'm running when try access this value dosomething method passed component, i'm getting window, not foodview instance. how can bind foodlist instance given situation?
## backbone view: foodlist = require '../components/foodlist' class foodview extends backbone.view initialize: => ...done() => @renderfood() renderfood: -> react.render( <foodlist foods={@collection.foods.tojson()} dosomething={dosomething} ## method of interest />, app.pageel ) dosomething: (e) -> console.log @ ## returns window. id = $(e.currenttarget).data('id') @collection.makeachange(id) ## want access foodview instance's collection ## react component: foodlist = react.createclass handlesomething: (e) -> @props.dosomething(e) render: -> return( ... <li data-id={id} onclick={@handlesomething}>dynamically generated</li> ... )
using coffeescript, need use fat arrow function automatically bind foodview::dosomething method instance. see bound functions.
class foodview extends backbone.view dosomething: (e) => // `@` refers foodview instance which equivalent to
class foodview extends backbone.view constructor: -> @dosomething = @dosomething.bind(@) dosomething: (e) -> // `@` refers foodview instance
Comments
Post a Comment