question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pass hash helper to link-to (EmptyObject lacking hasOwnProperty)

See original GitHub issue

Sometimes a route can resolve with multiple records like so:

model() {
  const profile = ...;
  const user = ...;

  return RSVP.hash({
    profile,
    user,
  });
},

So then you’d think you could use link-to like so:

{{link-to 'My Profile' 'profiles.show' (hash user=user profile=profile)}}

This doesn’t work because the hash helper returns EmptyObject and extractQueryParams tries to call .hasOwnProperty() on it, resulting in an error.

There’s a workaround to this which involves using an action with transitionTo but it feels like a poor experience.

This is an issue I’ve had with Ember for years. What’s the correct way to pass multiple records to link-to if not this?

I’m using ember-cli 3.0.2.


Edit: I’ve started using the following helper in place of hash as a workaround:

import { helper } from '@ember/component/helper';

export function plainObject(params, hash) {
  const object = {};
  // NOTE: `hash` is an `EmptyObject` so we convert to a POJO so that it can be
  // used with `link-to`.
  Object.keys(hash).forEach(k => object[k] = hash[k]);
  return object;
}

export default helper(plainObject);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
btecucommented, Oct 3, 2018

Define standard… It works right now but it’s been decided that in general it’s not a good pattern and in the future it might be no longer supported.

There was at least one RFC related to this, maybe more: https://github.com/emberjs/rfcs/pull/283

@mhluska Are you able to create a test replicating the issue?

1reaction
rwjbluecommented, Oct 3, 2018

@mhluska - I’m actually unable to reproduce the original issue, I wrote a test that should have failed but didn’t:

    ['@test [GH#17018] using `hash` helper for model']() {
      this.addTemplate('index', `{{link-to 'Post' 'post' (hash id="bar")}}`);
      this.addTemplate('post', 'Post: {{model.id}}');

      this.router.map(function() {
        this.route('post', { path: '/posts/:post_id' });
      });

      return this.visit('/')
        .then(() => {
          this.assertComponentElement(this.firstChild, {
            tagName: 'a',
            attrs: { href: '/posts/bar' },
            content: 'Post',
          });

          return this.click('a');
        })
        .then(() => {
          this.assertText('Post: bar');
        });
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Which object does not have `hasOwnProperty` in JavaScript?
Trying to look up a custom property in later code fails because a new, different wrapper object, lacking the custom property, is used...
Read more >
rollup.js
This allows you to build on top of existing tools and modules without adding extra dependencies or bloating the size of your project....
Read more >
password_hash - Manual - PHP
password_hash() creates a new password hash using a strong one-way hashing algorithm. ... The "add note" rules of php.net say I can't link...
Read more >
JavaScript Hash Table – Associative Array Hashing in JS
This tutorial will help you understand Hash Table implementation in JavaScript as well as how you can build your own Hash Table class....
Read more >
Why are hash functions one way? If I know the algorithm, why ...
Let me invent a simple "password hashing algorithm" to show you how it works. Unlike the other examples in this thread, this one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found