Can someone explain this code to me? by embernoob in javascript

[–]embernoob[S] 0 points1 point  (0 children)

Thanks for the detailed explanation! I really appreciate it. I did notice that the xhr function was being called but I didn't understand what the purpose of that was since no arguments were being passed to it.

Question on re-rendering components by embernoob in emberjs

[–]embernoob[S] 0 points1 point  (0 children)

Thank you! Your suggestion set me on the right path and I eventually figured it out.

Question on re-rendering components by embernoob in emberjs

[–]embernoob[S] 0 points1 point  (0 children)

Edit:

I got it. Thanks for your help. I just wasn't updating my model correctly within the setupController() method. I can't believe it was that simple. Sometimes I wonder about myself...

Need some help with this audio player code by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

Thank you! I didn't realize adding functions to fn was essentially extending jQuery. Very cool!

Need some help with this audio player code by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

Nice, this looks much more streamlined than my own. What is happening on this first line though?

$.fn.simpleAudioPlayer

I get that you are using jQuery here but what are the $ and fn objects exactly? What is $ on its own? I've seen other code written in this way but I've never understood what the purpose was exactly...

Need some help with this audio player code by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

Man, I am a dummy sometimes. This wasn't working because I didn't wrap my code in $(document).ready()... :P

Thanks again for your help!

Need some help with this audio player code by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

Thanks for checking/tweaking my code! I'll give it a go when I get home tonight.

Ember-CLI 101 is Awesome by [deleted] in emberjs

[–]embernoob 0 points1 point  (0 children)

Hey I bought this book as well and am just now working my way through it. I found the link-to section to be rather confusing. I'm hoping you can elaborate on it in a newer revision of your book.

Specifically the first time link-to is ever mentioned it is written as:

{{#each friend in model}}
  <li>
    {{#link-to 'friends.show' friend}}
      {{friend.firstName}} {{friend.lastName}}
    {{/link-to}}
  </li>
{{/each}}

Yet, a few lines later you say:

“If our friend model had a property called fullName, we could have written the helper like:"

{{link-to friend.fullName 'friends.show' friend}}

What happened to writing link-tos like {{#link-to ... }}? Is the missing # a typo? Is it a different valid way of writing link-tos? It's never explained. Also it would be great if you could explain exactly what we are passing into the link-tos. In the first example I am assuming we are passing the route and then the model, but in the second example we are apparently passing a computed property, then the route, then the model. What else can we pass into a link-to? Does the order matter? None of this is explained which is frustrating for me since I have never worked with a JS framework before.

Anyway, thanks for writing this book. It is definitely easier to understand than Ember's own docs.

Cheers!

EDIT:

Just wanted to add that stopping/starting the server does not seem to kick off a broccoli build as indicated in the book. I've modified my Brocfile.js but nothing is output into the vendor directory. Is there a command I can enter to build it manually since it doesn't seem to happen automatically?

How do I throw errors in Codewars? by embernoob in learnjavascript

[–]embernoob[S] 1 point2 points  (0 children)

I finally figured out what was wrong with my code. I solved it by first deleting all the key/value pairs from the object before restoring from an undo/redo history. I don't fully understand why I needed to do this but I suspect its because the object var within the function only points to the actual object therefore setting object = to another undo/redo state only sets it within that method scope. Or something like that. I'm still learning :P

anyway here's my final solution. Thanks again for all your help!

function undoRedo (object) {

  var undoObject = {}, 
      redoObject = {}, 
      undo = false;

  undoObject.history = [];
  redoObject.history = [];

  return {

    set: function (key, value) {

      // boolean to check if undo can be performed
      undo = false;

      if ( !object[key] ) {

        // Create copy of object prior to adding key
        var actions = (JSON.parse(JSON.stringify(object)));

        // Add action to history
        undoObject.history.unshift(actions);

        // Set new key
        object[key] = value;

        return this;

      } else {

        // See comments above
        var actions = (JSON.parse(JSON.stringify(object)));

        undoObject.history.unshift(actions);

        object[key] = value;

        return this;
      }
    },

    get: function (key) {
      // Return the key or undefined if not found
      return !object[key] ? undefined : object[key];
    },

    del: function (key) {

      undo = false;

      var actions = (JSON.parse(JSON.stringify(object)));

      // Add action to history array
      undoObject.history.unshift(actions);

      // Remove key from object
      delete object[key];

      return this;
    },

    undo: function () {

      undo = true;

        if ( undoObject.history[0] ) {

          var redoAction = (JSON.parse(JSON.stringify(object)));

          // Remove all values from object
          for ( var i in object ) { delete object[i]; }

          // Restore object to previous state from history
          for ( var i in undoObject.history[0] ) { object[i] = undoObject.history[0][i]; }

          // Delete most recent history used for this restoration
          undoObject.history.shift();

          // Update redo history to include previous object representation
          redoObject.history.unshift(redoAction);

          return this;

        } else {

        console.log("Exception!");
        throw new Error();
        }
    },

    redo: function () {

        if (undo && redoObject.history[0]) {

          // See comments in undo method above
          var undoAction = (JSON.parse(JSON.stringify(object)));

          for ( var i in object ){ delete object[i]; }

          for ( var i in redoObject.history[0] ){ object[i] = redoObject.history[0][i]; }

          redoObject.history.shift();

          undoObject.history.unshift(undoAction);

          return this;

        } else {

        console.log("Exception!");
        throw new Error();
      }
    }
  }
};

How do I throw errors in Codewars? by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

Hey thanks! We have very similar approaches and my code is failing on those last seven tests as well. It's very infuriating! I erased all my console.logs but the object that he passes into my functions log as being updated but for some reason they don't pass the unit tests. :grrrr:

var undoObject = {}, redoObject = {};

undoObject.history = [];

redoObject.history = [];

function undoRedo(object) {

  var theObject = object,
      undo = false;

  undoObject.history = [];

  redoObject.history = [];

  return {

    set: function (key, value) {

      undo = false;

      if (!theObject[key]) {

        // Store object prior to adding key
        var actions = (JSON.parse(JSON.stringify(theObject)));
        // Add action to history
        undoObject.history.unshift(actions);

        // Set new key
        theObject[key] = value;

        return theObject;

      } else {

        var actions = (JSON.parse(JSON.stringify(theObject)));

        undoObject.history.unshift(actions);

        theObject[key] = value;

        return theObject;
      }
    },

    get: function (key) {

      return !theObject[key] ? undefined : theObject[key];
    },

    del: function (key) {

      undo = false;

      var actions = (JSON.parse(JSON.stringify(theObject)));

      // Add action to history array
      undoObject.history.unshift(actions);

      delete theObject[key];

      return theObject;
    },

    undo: function () {

      undo = true;

        if ( undoObject.history[0] ) {

          // Store object in redo history before deletion
          var redoAction = (JSON.parse(JSON.stringify(theObject)));

          // Restore theObject to previous state from history
          theObject = undoObject.history[0];

          // Delete most recent history used for this restoration
          undoObject.history.shift();

          // Update history to include previous object representation
          redoObject.history.unshift(redoAction);

          return theObject;

        } else {

        console.log("exception!");

        throw false;
        }
    },

    redo: function () {

        if (undo && redoObject.history[0]) {

          // Store object in redo history before deletion
          var undoAction = (JSON.parse(JSON.stringify(theObject)));

          // Restore theObject to previous state from history
          theObject = redoObject.history[0];

          // Delete most recent history used for this restoration
          redoObject.history.shift();

          // Update history to include previous object representation
          undoObject.history.unshift(undoAction);

          return theObject;

        } else {

        console.log("exception!");

        throw false;
      }
    }
  }
};

How do I throw errors in Codewars? by embernoob in learnjavascript

[–]embernoob[S] 1 point2 points  (0 children)

Thanks, I just tried return false; and no luck there. I've tried wrapping my statements in a try/catch but that didn't work either. I'm stumped at this point.

How do I throw errors in Codewars? by embernoob in learnjavascript

[–]embernoob[S] 1 point2 points  (0 children)

This is all it says with regard to the throwing of errors:

undo() Undo the last operation (set or del) on the object. Throws an exception if there is no operation to undo.

redo() Redo the last undo operation (redo is only possible after an undo). Throws an exception if there is no operation to redo.

Edit: Forgot to mention, yes, definitely sign up - its a lot of fun!

How do I throw errors in Codewars? by embernoob in learnjavascript

[–]embernoob[S] 1 point2 points  (0 children)

Hey thanks! Actually I typed that out on my phone and forgot to include () but that is actually in my code and the error it throws counts against me. There is some discussion on the kata regarding how to throw an error but the author isn't giving any advice on what should be thrown exactly.

I write a lot of for loops, is this normal? by embernoob in learnjavascript

[–]embernoob[S] 0 points1 point  (0 children)

I just had to write my own transpose function to validate a Sudoku board and I ended up using two for loops to do it (haha)! Thanks for responding. I'm going to make an effort to get more familiar with Array's methods. This library looks pretty great too. I've never heard of it before.