'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - Ember.js computed property from store - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember.js computed property from store - Stack Overflow

programmeradmin0浏览0评论

I'm trying to get a simple count of objects returned by REST get request from the server to use in another controller in Ember.js

For this reason I need to make an additional request to the server. Basically here's my code and it almost works.. but not quite yet. Maybe someone can figure out why.

It return a PromiseArray, that's why I'm using .then() to access the properties .

App.TestController = Ember.ObjectController.extend({
    totalCount: function() {
        return this.store.find('question', {test: this.get('id')}).then(function(items) {
            var count = items.get('content').get('length');
            console.log(count); // This actually logs correct values
            return count;
        })
    }.property('question')
})

It does what it suppose to do and I'm getting correct values printed out in the console.log(), but when I try to use {{totalCount}} in the view template I'm getting [object Object] instead of an integer.

Also, am I properly observing the questions property? if the value changes in its proper controller will the value update?

Thanks

I'm trying to get a simple count of objects returned by REST get request from the server to use in another controller in Ember.js

For this reason I need to make an additional request to the server. Basically here's my code and it almost works.. but not quite yet. Maybe someone can figure out why.

It return a PromiseArray, that's why I'm using .then() to access the properties .

App.TestController = Ember.ObjectController.extend({
    totalCount: function() {
        return this.store.find('question', {test: this.get('id')}).then(function(items) {
            var count = items.get('content').get('length');
            console.log(count); // This actually logs correct values
            return count;
        })
    }.property('question')
})

It does what it suppose to do and I'm getting correct values printed out in the console.log(), but when I try to use {{totalCount}} in the view template I'm getting [object Object] instead of an integer.

Also, am I properly observing the questions property? if the value changes in its proper controller will the value update?

Thanks

Share Improve this question edited Feb 26, 2014 at 17:51 kernelpanic asked Feb 23, 2014 at 19:54 kernelpanickernelpanic 2,9563 gold badges37 silver badges61 bronze badges 1
  • possible duplicate of Rendering resolved promise value in Ember handlebars template – aemxdp Commented Feb 23, 2014 at 20:01
Add a ment  | 

2 Answers 2

Reset to default 8

The problem you are seeing is because your are returning a promise as the value of the property and handlebars won't evaluate that promise for you. What you need to do is create a separate function that observes question and then call your store there to update the totalCount-property. It would be something like this.

App.TestController = Ember.ObjectController.extend({
    totalCount: 0,
    totalCountUpdate: function() {
        var that = this;
        this.store.find('question', {test: this.get('id')}).then(function(items)     {
            var count = items.get('content').get('length');
            console.log(count);
            that.set('totalCount', count);
        })
    }.observes('question')
})

Alternatively totalCount might lazily set itself, like this:

App.TestController = Ember.ObjectController.extend({
    totalCount: 0,
    question: // evaluate to something,
    totalCount: function() {
        var that = this;
        that.store.find('question', {test: that.get('id')}).then(function(items)     {
            var count = items.get('content').get('length');
            that.set('totalCount', count);
        })
    }.observes('question').property()
})
发布评论

评论列表(0)

  1. 暂无评论