1

Using ember data, I would like to find the id of a model, using a slug. I have been following the approach found on pixelhandler's ember blog, but this will not work for my server, which does not support query params like blog/post?slug=mypost.

I am able to serialize the correct URLs for my blog posts, but Ember fails to find the correct model on refresh.

My post.js route:

  import Ember from "ember";
  import SlugRouteableMixin from "../../mixins/slug-routeable";
  var PostsPostRoute;

  PostsPostRoute = Ember.Route.extend(SlugRouteableMixin, {
    model: function(params) {
      return new Ember.RSVP.Promise((function(_this) {
        return function(resolve, reject) {
          var found;
          found = _this.store.filter("post", function(post) {
            console.log("Comparing post with slug " + (post.get("slug")) + " against the url slug " + params.post_slug);
            console.log(post.get("slug") === params._post_slug);
            return post.get("slug") === params.post_slug;
          });
          if (found.get("length") > 0) {
            return resolve(found[0]);
          } else {
            console.warn("failed to match against the slug found was:");
            console.dir(found.get("length"));
            return _this.store.find("post", params.post_slug).then((function(post) {
              return resolve(post);
            }), function(error) {
              return reject(error);
            });
          }
        };
      })(this));
    }
  });

  export default PostsPostRoute;

Which results in:

Comparing post with slug the-new-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.859false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.859Comparing post with slug another-test-blog-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.859false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug undefined against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860true blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug ember-latest against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug ddddd against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug test-title against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug another-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug latest-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug my-test-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug slug-test against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug gretchin against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug undefined against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860true blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.860Comparing post with slug new-post against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.860false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.861Comparing post with slug title against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.861false blog-app/routes/posts/post.js:16
2014-11-29 20:37:28.861failed to match against the slug found was: blog-app/routes/posts/post.js:22
2014-11-29 20:37:28.8610 blog-app/routes/posts/post.js:23
2014-11-29 20:37:28.863Comparing post with slug undefined against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:28.863true blog-app/routes/posts/post.js:16
2014-11-29 20:37:29.823Comparing post with slug undefined against the url slug another-test-blog-post blog-app/routes/posts/post.js:15
2014-11-29 20:37:29.823true blog-app/routes/posts/post.js:16
2014-11-29 20:37:29.824Error while processing route: posts.post Not found: type "post" with id "another-test-blog-post" Error: Not found: type "post" with id "another-test-blog-post"
    at ``http://localhost``:4200/assets/vendor.js:96869:13 

It appears that store.filter is not returning an expected Array object, but that the filtering is still occurring.

How can I filter my models by slug such that I can get the ID of a model by slug?

2
  • What format does your api want the request in? Nov 30, 2014 at 4:49
  • @NicholasJohn16, something liks this: http://localhost:5984/blog-app/_local/duG9i1y5jhCLiLl6bo4_jw==?_nonce=zOBXiThr154aotPr, where the format is http://localhost:5984/blog-app/_local/{id} Nov 30, 2014 at 8:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.