mercredi 3 février 2016

EmberJS 2.3.0 reusable functions

So I am building a sidebar as a component. That component loops thru an array of dates. So on the index page I pass the model to the component. But I do not want to build the model on every page. Is there a way to make component specific models?

{{main-sidebar model=model.meetingDates}}

The data that is passed into the component is an array of strings. The model is built via JS. The JS functions I wrote generate 3 dates and return them in an array. Code can be viewed here --> http://ift.tt/1TFreZj

var getThirdWed = function(customDate, currentDate, wedCount) {

    // Keep uping the date of the month till we hit a wed
    while (customDate.getDay() !== 3) {
        customDate.setDate(customDate.getDate() + 1);
    }

    // Set date based of wedCount
    customDate.setDate(customDate.getDate() + (7 * (wedCount - 1)));

    if (customDate.getTime() > currentDate) {
        var month = customDate.toLocaleString("en-us", {
            month: "long"
        });
        var ordinal = "th";

        if (customDate.getDate() < 3) {
            ordinal = "st";
        }

        if (customDate.getDate() > 20) {
            ordinal = "st";
        }

        return month + " " + customDate.getDate() + "" + ordinal + " at 7:15pm";
    }

    return "";
};

var getNextMeeting = function(meetingCount, wedCount) {
    var now = new Date();
    var currentDate = now.getTime();
    var customDate = now;

    // Set the date to the first of the month
    customDate.setDate(1);

    // If meetingCount is 0 that means we want the current month
    if (meetingCount === 0) {
        return getThirdWed(customDate, currentDate, wedCount);
    } else {
        // Set to a future month
        customDate.setMonth(customDate.getMonth() + meetingCount);

        return getThirdWed(customDate, currentDate, wedCount);
    }

    return "";
};

$(function() {
    var meetingDates = [];
    var count = 0;

    while (count < 3) {
        var date = getNextMeeting(count, 3);

        if(date !== '') {
            meetingDates.push(date);
            count++;
        }
    }

    $('.text').html(meetingDates);
    console.log(meetingDates);
});

Thoughts?




Aucun commentaire:

Enregistrer un commentaire