EmberJS + ember数据:如何通过slug过滤ID

时间:2014-11-30 03:50:28

标签: javascript ember.js ember-data

使用ember数据,我想使用slug找到模型的id。我一直关注the approach found on pixelhandler's ember blog,但这不适用于我的服务器,它不支持blog/post?slug=mypost等查询参数。

我可以为serialize我的博客帖子找到正确的网址,但是Ember在刷新时无法找到正确的model

我的post.js路线:

  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;

结果是:

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 

store.filter似乎未返回预期的Array对象,但过滤仍在进行。

如何按slug过滤模型,以便ID能够获得模型的slug

0 个答案:

没有答案