Ember CLI - 自定义DS.LSAdapter.extend返回undefined

时间:2015-01-20 13:32:01

标签: ember-data ember-cli

我想使用自定义LSAdapter在localstorage中保存声明。

app / adapters文件夹有两个适配器:application.js和claim.js。登录后我想在de store中保存de检索到的声明,如下所示:

claimarr.forEach(function(claimString){

var claimsplitted = claimString.split(" ");

var record = store.createRecord('claim', 
      {
        'type' : claimsplitted[0],
        'value': claimsplitted[1]
      }
    );
 record.save();

});

我无法创建自定义LSAdapter。我正在使用Simple Auth,插件将数据写入localStorage!

适配器/ application.js中

import DS from "ember-data";

 export default DS.RESTAdapter.reopen({
    namespace: 'api/',
    host: 'http://localhost:1336',
    headers: function() {
        return {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "application/json"
        };
    }.property().volatile()
});

adapters / claim.js

import DS from "ember-data";

 export default DS.LSAdapter.extend({
    namespace: 'api/'
    }
);

Brocfile.js

..    

app.import("bower_components/ember-localstorage-adapter/localstorage_adapter.js");

module.exports = app.toTree();

models / claim.js

import DS from "ember-data"; 

export default  DS.Model.extend({

    type : DS.attr('string'),
    value : DS.attr('string'),

});  

更新
我的logincontroller中的解决方法:

controllers/login.js  
actions: {

    login: function(){
      //this.set('currentDate', moment());
      //var now = moment;
        //console.log(this.now);

      var self = this;
      self.set('errorMessage', null);

        this.get('session').authenticate(this.authenticator, {
            identification: this.get('identification'),
            password: this.get('password')
          })

        .then(

        function() {

          function getClaimSet(session){

              var claimSet = [];


              //build claimSet from base64 string
              var claimarr = window.atob(session.content.claims).split(",");

              claimarr.forEach(function(claimString){

                var claimsplitted = claimString.split(" ");
                var record = 
                      {
                        'type' : claimsplitted[0],
                        'value': claimsplitted[1]
                      };

                 claimSet.push(record);

              });

              return claimSet;

          }

        var session = self.get('session');
        var claimSet = getClaimSet(session);
        session.set("claimSet" , claimSet );

        session["hasClaim"] = function(value, type){

          if(typeof(type) === "undefined"){
            type = "Method";
          }

          var foundArr = session.content.claimSet.filter(function(claim){return claim.type === type && claim.value === value;});
          return foundArr.length > 0;

        };

我现在可以使用:session.hasClaim(value, type)

0 个答案:

没有答案