Note that there are some explanatory texts on larger screens.

plurals
  1. POIdiomatic way to listen to model changes
    primarykey
    data
    text
    <p>I'm playing with React for the first time and I think I really like it. I've implemented (large parts of) the board game <a href="http://en.wikipedia.org/wiki/Go_%28game%29" rel="nofollow">Go</a> with it and so far, but I've run into something strange that I don't know how to approach in the idiomatic React way. Basically, I've got a model--the board game--implemented as its own class <a href="https://github.com/cjlarose/react-go/blob/master/board.js" rel="nofollow">Board</a>. It exposes only it's constructor, and methods <code>play(i,j)</code> and <code>pass</code>. It handles all of the game logic and updates its own internal state appropriately. It has no reference to anything related to a view/component. I've got a React Component called <a href="https://github.com/cjlarose/react-go/blob/master/go.js#L24-L49" rel="nofollow">BoardView</a> which maintains a reference to an instance of a <code>Board</code>. I've also got a Component called <code>AlertView</code> that displays messages about the game state (illegal moves and such) when appropriate.</p> <p>Everything works well now, and I like the separation of concerns between the <code>Board</code> class and its views. However, the way I have my <code>Board</code> class communicate its changes to the views is unusual, and I feel that it is inconsistent with other React code. Basically, I abuse <a href="http://api.jquery.com/trigger/" rel="nofollow">jQuery's event system</a> to allow me to trigger arbitrary events like <code>["update", "atari", "suicide"]</code>. In this scheme, the Component has an onClick listener that calls <code>Board.play</code>, which triggers 0 to many events on the Board instance. The Component listens for an <code>"update"</code> event, and calls <code>this.setState</code>, which will force it to re-<code>render()</code>, putting the view into a state that correctly depicts the game. The <code>AlertView</code> listens for the <code>"atari"</code> and <code>"suicide"</code> events on the same board instance and similarly calls <code>this.setState</code>, which triggers another <code>render()</code>.</p> <p>Should I cut out the jQuery events? If so, what's the best way of doing this?</p> <p>All code is available <a href="https://github.com/cjlarose/react-go" rel="nofollow">here</a> and you can play with the app <a href="http://cjlarose.com/react-go/" rel="nofollow">here</a>.</p> <p>Edit: For posterity's sake, this question was asked at commit <a href="https://github.com/cjlarose/react-go/tree/3f600ce2d58b37a5e8637bd314ce614d27505e96" rel="nofollow">3f600c</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload