簡體   English   中英

使用Meteor中的事件從數組中檢索元素

[英]Retrieving elements from an array using events in Meteor

我從[categories]函數中獲取[object,object]而不是數組中的元素。 盡管我不確定,但這也許是預料之中的,但我只是弄錯了。

if (Meteor.isClient) {

    Template.ipsosboard.helpers({
         'categories': function() {
        return array; // Some data stored as JS Object in lib.
    },
        'currentElement': function() {
            return Session.get('selectedEvent');
        }
    });

    Template.ipsosboard.events({
        "change #category-select": function(event, template) {
            var selectedEvent = $(event.currentTarget).val();
            Session.set('selectedEvent', selectedEvent);
            console.log("EventNum: " + selectedEvent);
        }
    });

}; //end of client code.

if (Meteor.isServer) {
    //code to run by server here.
};

這似乎可以解決它。 它需要將對象轉換為數組。 注意:“數據”是作為JS對象存儲在項目的lib文件夾中的json文件,因此我需要對其進行轉換。

if (Meteor.isClient) {

    Template.ipsosboard.helpers({
         'categories': function() {

需要使用以下函數將其轉換為數組。

        var myObj = data;
        var array = $.map(myObj, function(value, index) {
            return value;
        });
        return array;
    },

因此,現在它根據需要返回數據。

        'currentElement': function() {
            return Session.get('selectedEvent');
        }
    });

    Template.ipsosboard.events({
        "change #category-select": function(event, template) {
            var selectedEvent = $(event.currentTarget).val();
            Session.set('selectedEvent', selectedEvent);
            console.log("EventNum: " + selectedEvent);
        }
    });

}; //end of client code.

if (Meteor.isServer) {
    //code to run by server here.
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM