【问题标题】:Ember.js looks for model of first data property returned from webapi controllerEmber.js 查找从 webapi 控制器返回的第一个数据属性的模型
【发布时间】:2014-08-14 10:15:59
【问题描述】:

任何人都可以解决这个我已经挣扎了几个小时的问题,这将是很棒的......

我是 ember.js 的新手,我正在尝试通过以下方式将用户模型加载到我的应用程序的全局范围中:

(安装程序基于this template 并从那里使用 webapi_adapter 和序列化程序。)

据我所知,我需要使用以下内容:

ApplicationController.js

App.ApplicationController = Em.ObjectController.extend({
    hasError: function () {
        var currentError = this.get("error");
        return !(currentError === '' || currentError === null);
    }.property('error'),
});

User.js

var attr = DS.attr;
App.User = DS.Model.extend({
    email: attr('string'),
    accountId: attr('string'),
    firstName: attr('string'),
    lastName: attr('string'),
});

App.UserSerializer = DS.WebAPISerializer.extend({
    primaryKey: 'email',

    normalizeHash: {
        user: function (hash) {
            hash.email = hash.email;
            return hash;
        },
    }
});

ApplicationRoute.js

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

UserController.cs

//GET api/user
        public object GetCurrentUser()
        {
            var u = AuthenticatedUser;
            return new UserDto(u);
        }

UserDto.cs

public class UserDto
{
    public UserDto() { }

    public UserDto(User user)
    {
        Email = user.Email;
        FirstName = user.FirstName;
        LastName = user.LastName;
        AccountId = user.AccountId;
    }

    [Key]
    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string AccountId { get; set; }

}

我遇到的问题是我得到了错误

Error while processing route: index No model was found for 'email' Error: No model was found for 'email'

其中 email 是用户数据中返回的第一个 json 属性,即:

{"Email":"johnsmith@test.com","FirstName":"John","LastName":"Smith","AccountId":"AccountJS"}

任何想法我做错了什么?

仅供参考:

router.js

App.Router.map(function () {
    this.route("index", { path: "/" });
});

【问题讨论】:

    标签: c# javascript asp.net ember.js asp.net-web-api


    【解决方案1】:

    我做了以下显示用户列表,你可以参考:

    App\controllers\UserListsController.js

    App.UserListsController = Ember.ArrayController.extend({
        error: ""
    });
    

    App\models\UserList.js

    var attr = DS.attr;
    
    App.UserList = DS.Model.extend({
        email: attr('string'),
        accountId: attr('string'),
        firstName: attr('string'),
        lastName: attr('string'),
    });
    
    App.UserListSerializer = DS.RESTSerializer.extend({
        primaryKey: 'email',
    
        //// ember-data-1.0.0-beta2 does not handle embedded data like they once did in 0.13, so we've to update individually if present
        //// once embedded is implemented in future release, we'll move this back to WebAPISerializer.
        //// see https://github.com/emberjs/data/blob/master/TRANSITION.md for details
        extractArray: function (store, primaryType, payload) {
            var primaryTypeName = primaryType.typeKey;
    
            var typeName = primaryTypeName,
                type = store.modelFor(typeName);
    
            var data = {};
            data[typeName] = payload;
            payload = data;
            return this._super.apply(this, arguments);
        },
    
        normalizeHash: {
            userList: function (hash) {
                hash.email = hash.id;
                return hash;
            }
        }
    
    });
    

    App\routes\TodoListRoute.js,添加

    App.UserListsRoute = Ember.Route.extend({
        model: function () {
            return this.store.find('userList');
        },
    });
    

    App\templates_navbar.hbs,添加

    <li>
        {{#linkTo 'userLists'}}
          userList
        {{/linkTo}}
    </li>
    

    应用\模板\userLists.hbs

    <h2>Users</h2>
    
    <section id="lists">
        {{#each controller}}
        <article class="todoList">
            <p>{{email}}</p>
            <p>{{firstName}}</p>
            <p>{{lastName}}</p>
        </article>
        {{/each}}
    </section>
    

    App\router.js,添加

    this.route("userLists", { path: "/userList" });
    

    Controllers\UserListController.cs,示例:

    public class UserListController : ApiController
    {
        private TodoItemContext db = new TodoItemContext();
    
        // GET api/userList
        public List<UserDto> GetUserLists()
        {
            var u = new User
            {
                Email = "test@test.com",
                FirstName = "first",
                LastName = "last",
                AccountId = "1324"
            };
            List<UserDto> users = new List<Models.UserDto>();
            users.Add(new UserDto(u));
            return users;
        }
    
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
    

    模型\User.cs

    public class User
    {
        public User() { }
    
        [Key]
        public string Email { get; set; }
    
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    
        public string AccountId { get; set; }
    }
    
    
    public class UserDto
    {
        public UserDto() { }
    
        public UserDto(User user)
        {
            Email = user.Email;
            FirstName = user.FirstName;
            LastName = user.LastName;
            AccountId = user.AccountId;
        }
    
        [Key]
        public string Email { get; set; }
    
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    
        public string AccountId { get; set; }
    }
    

    【讨论】:

    • 你是我的朋友的救世主!
    【解决方案2】:

    Ember.js 似乎希望您的 API 响应数据如下所示:

    { 
        "user": {
            "Email": "johnsmith@test.com",
            "FirstName": "John",
            "LastName": "Smith",
            "AccountId": "AccountJS"
        }
    }
    

    我不知道你提到的webapi_adapter,但这就是RESTAdapter 期望响应的格式:http://emberjs.com/api/data/classes/DS.RESTAdapter.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 2016-09-24
      相关资源
      最近更新 更多