如何使用Meteor JS在HTML中获取数组数据?

时间:2014-04-10 14:06:31

标签: collections meteor

我需要知道使用Meteor JS获取集合的数组数据。我做了一个简单的例子,将数据插入Meteor JS中的集合,如下所示:

if (Hcare_Fileds.find().count() === 0) {

    var fieldData = [

      {fieldName: "Hcare1",

       fieldOptions: [ "Bike","Car","TV","Radio","etc"]

      },

      {fieldName: "Hcare2",

       fieldOptions: [ "Bike1","Car1","TV1","Radio1","etc"]
       },

     {fieldName: "Hcare3",

       fieldOptions: [ "Bike2","Car2","TV2","Radio2","etc"]
      }

    ];



    for (var i = 0; i < fieldData.length; i++) 
   {

      var list_id = Hcare_Fileds.insert({fieldname: fieldData[i].fieldName,fieldoptions: fieldData[i].fieldOptions}
      , function( error, result)  
        {  
                  if ( error ) console.log ( "Error :"+error.reason ); //info about what went wrong 
              if ( result )  
            { 
               console.log ( "Success="+result );//the _id of new object if successful 

                }                     
               });

    }
  }

并访问上面显示的上述集合fieldoption数组sa:

 {{#each fieldName}}
        <div class="fmenu {{isselected}}">  {{ fieldname }}  </div>

                  <div class="foptions"> 

                    {{#if isselected}}

                           {{ fieldoptions }}
 // Here get fieldoptions data when selected filedname as Bike,Car,Tv,Radio,etc but i need to return like as array but it returns string 

                     {{/if}}

                  </div>

       {{/each}}

JS代码:

if (Meteor.isClient) 
{
  Session.set('currentFieldName', '');

  Template.main.fieldName = function () 
  {

     return Hcare_Fileds.find().fetch(); 
  };



  //TODO :: 

  Template.main.events
  ({
    'click .fmenu': function (e,t)
    {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button"+this.fieldname);
        e.preventDefault();
        Session.set('currentFieldName', this.fieldname);
    }
  });

  Template.main.isselected = function () 
  {
     console.log("currentFieldName"); 
     return Session.equals("currentFieldName", this.fieldname) ? "selected" : ''; 
  };

}

我需要将数据作为数组而不是字符串,如Bike,Car,Tv,Radio等。我对此一无所知。所以,请你建议我该怎么做?

0 个答案:

没有答案