Search code examples
backbone.jstitaniumtitanium-mobilefetchtitanium-alloy

Issue with where condion in titanium alloy


How can we fetch the values based on the condition, I tried as follows

var countrynames = Alloy.Collections.countryDetails;
countrynames.fetch({languageID: 1});
countrynames.on("reset", function() {
   var countrynamesLength = countrynames.length,
       column = Ti.UI.createPickerColumn();
   for (var i = 0; i < countrynamesLength; i++) {
      var row = Ti.UI.createPickerRow({
         title: countrynames.at(i).get("countryText")
     });
     column.addRow(row);
   }

   $.countryPicker.columns = [column];
});
countrynames.fetch({languageID: 1});

But the above does not filter the condition. It is fetching all the values from the table. How to use the where condition for the above code.

please any suggestions..


Solution

  • collection fetch method just grab data from your table.

    //grab all data no filter here.
    countrynames.fetch({
           success:function(){
              Ti.API.info('fetched');
           },
           error:function(){
              Ti.API.info('not fetched');
           }
         });
    

    collection where method grab specific data based on your filter it return a JavaScript array

    //grab data where languageID == 1
    countrynames.fetch();
    var filtered=countrynames.where({
            languageID: 1
         });
     //use data 
     Ti.API.info('filtered[0] =='+ JSON.stringify(filtered[0]));
    

    You can combine the two method using the query parameeter.

     countrynames.fetch({
           query::"SELECT * FROM countrynames(just put your table name here) WHERE   languageID ='1';"
      });
    

    So you can get an alloy collection containing your filtered data...

    countrynames.each(function(model){
          Ti.API.info('model '+ JSON.stringify(model));
    });
    

    Sorry for my english ...