hashable icon indicating copy to clipboard operation
hashable copied to clipboard

Given param is undefined

Open piotrkochan opened this issue 11 years ago • 8 comments

var h = hashable.hash(); h.update({dd:23425}); h.write();

jQuery version: v2.1.1

Trace:

Uncaught TypeError: Cannot set property 'dd' of null hashable.js:399 (anonymous function) hashable.js:399 hashable.extend hashable.js:396 hash.update hashable.js:34 Catalogue.setHash c.js:359 (anonymous function) c.js:466 jQuery.event.dispatch c.js:4409 elemData.handle

piotrkochan avatar May 06 '14 09:05 piotrkochan

This happens because the default hash data is null. There are a few ways to avoid this:

  1. Initialize the data with hash.data({}) first.
  2. Parse the current URL (assuming that it's valid) with hash.read() before calling update.
  3. If you don't care what's in the URL or what data has been set previously, use hash.set({dd:23425}) instead to overwrite it.

Make sense?

shawnbot avatar May 06 '14 15:05 shawnbot

Yep, thanks for reply

piotrkochan avatar May 06 '14 16:05 piotrkochan

The rationale for the default hash data to be null didn't make too much sense to me and I had a similar sort of hiccup when implementing into my project... just another data point :)

yelper avatar May 06 '14 16:05 yelper

Thanks for the feedback, @yelper. My rationale for initializing it as null was to be able to tell whether data had been set so that you can specify a default value, which is important when you're working in the context of a web app that's entirely driven by the URL (and, to a large extent, its validity). I hadn't really considered the use case for calling hash.update() right off the bat, but I think that's more a failure on my part to be explicit about the intended use case, and its associated boilerplate:

var hash = hashable.hash()
  .change(function(e) {
    // do something with e.data
  })
  .enable()
  .check();

I'm definitely interested in your and @pikey666's first impressions, though. Can you tell me more about how you expected it to work? Or is this simply a matter of expecting that hash.update() should force the data to be an object (or the default)?

shawnbot avatar May 06 '14 18:05 shawnbot

I also just realized that the first example in the README suggests the usage that you both probably tried out, and produces this exact error. So that's my bad, and I need to fix that first and foremost.

shawnbot avatar May 06 '14 18:05 shawnbot

As you say, I tried to do it as shown in the README.

piotrkochan avatar May 06 '14 18:05 piotrkochan

Yes, that's exactly what I was going to say, regarding the example in the README! With the use case you described as well (is a hash valid?), it makes complete sense why the data would be null initially. A little sentence in the README would probably help this misunderstanding.

For me, I ran into the issue when setting a default hash if the page had no hash parameters given. I tried to use hash.update() instead of setting a default with hash.default() and letting the default() method handling the no-parameters-given case.

yelper avatar May 06 '14 18:05 yelper

Okay, I've fixed the README, replacing the reference to hash.update() with hash.set(), added a test case for exactly this behavior, and minted v1.5.2. Let me know if this works for you guys and I'll close this. Thanks for pointing out my mistake!

shawnbot avatar May 06 '14 19:05 shawnbot