Note that there are some explanatory texts on larger screens.

plurals
  1. POSencha Touch 2 - Correct way to add listeners in the MVC?
    primarykey
    data
    text
    <p>Today I have a small question regarding what the correct/best way is to add listeners using Sencha Touch 2 via it's recommended MVC model.</p> <p>As far as I can see, there are two main ways that I have been presented with to add listeners to the various components across my views.</p> <p><strong>1. In the controller.</strong></p> <p>I came across this method while reading the MVC documents for ExtJS 4.0 (no MVC docs for touch yet). It goes like so:</p> <pre><code>init: function() { console.log ('Launched the controller'); //listening test //the Control function/method is unique to controllers and is used to add listeners to stuff this.control({ 'button': { 'tap' : function (){ console.log('the buttons speak!'); } }, } </code></pre> <p>The above code would reside inside the main controller, for instance. Here, as you can see, I am adding a "tap" listener to ALL buttons across the entire app. </p> <p>As far as I know, to access specific components in this way, I would need to tag them each with a unique ID and then use componentquery at this location to place listners onto them.</p> <p>Question: I think this method is pretty cool... but I have run across problems using it in lists... sometimes I want to listen to a specific list item for things like "tapstart" and "tapend" but since usually listItems are dynamically created as children to a list... I have no idea how to give them unique IDs and/or find them using the query engine (due to my inexperience I guess? I haven't been able to google/find anything about it in the docs that makes sense).</p> <p><strong>2. During the init/config of individual components</strong></p> <p>The other method that I came across to add listeners to components is to define the listener, it's callback and the event it's listening to directly in the component config.</p> <p>Example: </p> <pre><code>Ext.define('Paythread.view.CommentList', { extend: 'Ext.Panel', alias: 'widget.CommentList', layout: 'vbox', config : { items: [ { xtype: 'list', layout: 'fit', //fullscreen: true, height: 'viewport.height', store: 'Comments', onItemTap: function(){ //do stuff }, pressedDelay: 20, //HOLY CRAP IMPORTANT FOR UX itemTpl: '&lt;h1&gt;{user_id}&lt;/h1&gt;&lt;h2&gt;{comment}&lt;/h2&gt;' } ] }, }); </code></pre> <p>As you can see from this code, I have created a "onItemTap" listener function, and this seems to work pretty darn well. However... it scares me for some reason and I have no idea if what I am doing is correct or not.</p> <p>Could anyone provide some help as to whether I am doing the right thing, if I should be doing this a different way, or if I am completely off track and shouldn't even be defining listeners like this in the first place?</p> <p>I would really appreciate any help given! Thank you very much everyone.</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