samedi 23 juillet 2016

Using actions inside of a controller?

I'm new to ember and I would like to use a separate action (shown as shuffle) in one of my actions(assign). I know I'm doing something wrong here, the shuffle method isn't really doing anything. Am I calling it incorrectly or are actions not supposed to be used by other actions? Here is the code:

import Ember from 'ember';

export default Ember.Controller.extend({

taskData: [],
personData: [],
taskIn: ' ',
personIn: ' ',



  actions: {

    saveTask() {

        const task = this.get("taskIn");
        this.taskData.push(task);
    },

    savePerson()
    {
        const person = this.get("personIn");
        this.personData.push(person);
    },

    print(){
        var taskString;

        //this.taskData.remove(0);
        for(var i = 0; i < this.taskData.length; i++)
        {
            taskString = taskString + this.taskData[i];
        }
        alert(taskString);
        //alert(this.personData);
    },

    shuffle(array) {
        var currentIndex = array.length, temporaryValue, randomIndex;

        // While there remain elements to shuffle...
        while (0 !== currentIndex) {

            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;

            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }

        return array;
    },

    //algorithm to match up tasks with people
    assign(){


        var newTaskD = this.shuffle(this.taskData);
        var newPersonD = this.shuffle(this.personData);
        var taskString = '';
        var peopleString = '';

        for(var i = 0; i<newTaskD.length; i++)
        {
            taskString += " " + newTaskD[i];

        }

        for(var j = 0; j<peopleString.length; j++)
        {
            peopleString += " " + newPersonD[j];

        }

        alert(peopleString);
        alert(taskString);

    }
  }

});




Aucun commentaire:

Enregistrer un commentaire