如何在Ember.js和Ember数据中将对象用作模型属性

时间:2014-09-23 21:46:10

标签: javascript ember.js ember-data

我正在开发一个向用户显示中文字符的flashcard应用程序。 我有一个包含所有属性的App.User类设置,但是我很难跟踪试用属性,它跟踪用户暴露于特定字符/正弦图的次数。

应用程序控制器应该将当前登录的用户及其详细信息存储为属性。但我似乎无法掌握试验属性中的对象或如何在ember和ember数据中执行此操作。我读了一些关于扩展支持的属性类型的内容,但老实说我不知道​​如何使其工作。 (http://www.kaspertidemann.com/representing-objects-in-ember-data) 最终REST-api php-backend将提供数据,但是现在我使用ember数据的fixtures.adapter在我的应用程序中测试它。 非常感谢任何帮助!提前完成。

PS:我尽量做到尽可能完整,但如果有什么不清楚,请告诉我,我会提供更多细节!

// This template has a button which trigger the login action
<script type="text/x-handlebars" id="application">
    <div class="content">
        <form class="input-group">
            <div class="input-row">
                <label>Username</label>
                {{input type="text" value=username placeholder='username'}}
            </div>
            <div class="input-row">
                <label>Password</label>
                {{input type="password"  value=password placeholder='password'}}
            </div>
            <button {{action 'login'}} class="btn btn-primary btn-block btn-outlined">Login</button>
        </form>
    </div>
</script>

// application controller
    App.ApplicationController = Ember.ArrayController.extend({
    username: 'Sunchild',
    currentUser: {},
    // get the user.model
    particularUser: function() {
        return this.get('userArray').filterBy('username', this.get('username'));
        }.property('userArray.@each.username'),
    login: function(){
        if ((this.get('particularUser').getEach('hed_password') == _password)){
        // Code to store all the App.user info on the controller.
        // I figured I just set the currentUser property to the object, 
        // but I don't know how to get to the currentUser.trials property.
        this.set('currentUser', this.get('particularUser'));
        var _trialsObject = this.get('currentUser').getEach('trials');
        for (var key in _trialsObject) {
            // Does not work
        }
    }
});

// Router and Routes
App.Router.map(function() {
    this.resource('signup');
    this.resource('login');
    this.resource('profile');
    this.resource('practice');
    this.resource('overview');
});

App.ApplicationRoute = Ember.Route.extend({
    model: function () {
        return this.store.find('user');
    }
});

// The trials property is an object which should keep count of the number fo times the user has seen the flashcard with this character/sinogram
(function () {
    'use strict';

    App.User = DS.Model.extend({
        id: DS.attr('number'),
        username: DS.attr('string'),
        firstname: DS.attr('string'),
        lastname: DS.attr('string'),
        email: DS.attr('string'),
        hed_password: DS.attr(),
        trials: DS.attr(),
    });
})();

App.User.FIXTURES = [
 {
    id: 1,
    username: 'LargeSushi',
    firstname: 'Secil',
    lastname: 'Calzone',
    email: 'Secil@gmail.com',
    hed_password: 5432,
    trials: {"U+73ED": 1, "U+554A": 1}
    },
 {
    id: 2,
    username: 'Sunchild',
    firstname: 'Xiao',
    lastname: 'Feng',
    email: 'Sunchild@gmail.com',
    hed_password: 8754,
    trials: {"U+73ED": 1, "U+554A": 1}
 }
];

0 个答案:

没有答案