简体   繁体   English

Ember Data Transform中的访问控制器属性

[英]Access controller properties within Ember Data Transform

I like to access a controller property within Ember Data Transform. 我喜欢在Ember Data Transform中访问控制器属性。 Is there anyway how to do that? 无论如何,该怎么做?

Perhaps a few words to my use case. 也许对我的用例来说几句话。 I like to create a custom attribute 'encryptedStrings' and use DS.Transform to encrypt / decrypt the string with a given key by using Stanford Javascript Crypto Library. 我喜欢创建一个自定义属性'encryptedStrings',并使用DS.Transform通过使用Stanford Javascript加密库对具有给定密钥的字符串进行加密/解密。 The key should by part a query param in the uri. 键应该部分地在uri中作为查询参数。

get method is defined in DS.Transform but I did not get further. get方法在DS.Transform中定义,但我没有进一步介绍。 Here the relevant parts of the code: 这里是代码的相关部分:

App.EncryptedStringTransform = DS.Transform.extend({
    deserialize: function(serialized) {
       var key = this.get('pollController.encryptionKey');
       return Ember.isNone(serialized) ? null : String( sjcl.decrypt( key , serialized) );
    },
    serialize: function(deserialized) {
       var key = this.get('pollController.encryptionKey');
       return Ember.isNone(deserialized) ? null : String( sjcl.encrypt( key , deserialized) );
    }
});

App.PollController = Ember.ObjectController.extend({
    queryParams: ['encryptionKey'],
    encryptionKey: 'default'
});

Instead of 代替

this.get('pollController.encryptionKey');

I also tried this ones: 我也尝试过这些:

console.log( this.get('controller.encryptionKey') );
console.log( this.get('controllers.poll.encryptionKey') );
console.log( this.get('controllers.pollController.encryptionKey') );

Interesting! 有趣! I wonder whether it's a good idea to have the encryption key be so exposed to be in the URL. 我想知道将加密密钥公开在URL中是否是一个好主意。 But, assuming this is what you want to do, instead of transforming the data as part of the serializer, I think what I would do is set the key as a property on the model (from the route where the queryParam is exposed). 但是,假设这是您要执行的操作,而不是将数据作为序列化程序的一部分进行转换,我想我要做的是将键设置为模型上的属性(从暴露queryParam的路由开始)。 Then, have the unencrypted values be computed properties on the model that are derived based on the key and the encrypted values, which would just be regular DS.attr() . 然后,让未加密的值成为基于密钥和加密后的值(通常为常规DS.attr()得出的模型上的计算属性。 Computed properties can be both getters and setters so if a new value is set on the unencrypted property, then you can just set the reencrypted value. 计算属性既可以是getter,也可以是setter,因此,如果在未加密的属性上设置了新值,则只需设置重新加密的值即可。

Update: 更新:

Here's an example of how you can write the computed property in a reusable way: 这是如何以可重用的方式编写计算属性的示例:

Ember.computed.encrypted = function(encryptedField, keyField) {
  return Ember.computed(encryptedField, keyField, function(key, decryptedValue) {
    var encryptKey = this.get(keyField), encryptedValue;
    //setter
    if (arguments.length == 2) {
      encryptedValue = Ember.isNone(decryptedValue) ? null : String( sjcl.encrypt( encryptKey , decryptedValue) );
      this.set(encryptedField, encryptedValue);
    }
    encryptedValue = this.get(encryptedField);
    return Ember.isNone(encryptedValue) ? null : String( sjcl.decrypt( encryptKey , encryptedValue) );
  });
};

Then, you can write your model like this: 然后,您可以这样编写模型:

App.SensitiveData = DS.Model.extend({
  encryptKey:      DS.attr('string'),
  encryptedField1: DS.attr('string'),
  encryptedField2: DS.attr('string'),
  decryptedField1: Ember.computed.encrypted('encryptedField1','encryptKey'),
  decryptedField2: Ember.computed.encrypted('encryptedField2','encryptKey')
});

Update 2: 更新2:

You can set the encryption key on your model via the Route, like this: 您可以通过Route在模型上设置加密密钥,如下所示:

App.SensitiveDataRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('sensitive-data', params.sensitive_data_id).then(function(sensitiveData) {
      sensitiveData.set('encryptKey', params.encryption_key);
      return sensitiveData;
    });
  }
});

Or, assuming the data isn't exposed via the route, but only through the controller, you can add an observer to the controller and set the value through the observer, like this (note, it's possible that this isn't even necessary and since the controller proxies to the model, as long as the attribute in the model matches the query param name specified in the controller, you don't even need to do the observer logic, I'd need to play around with it to say for sure): 或者,假设数据不是通过路由公开,而是仅通过控制器公开,则可以向控制器添加观察者,并通过观察者设置值,如下所示(请注意,这甚至可能不是必需的,并且由于控制器代理到模型,因此只要模型中的属性与控制器中指定的查询参数名称匹配,您甚至不需要执行观察者逻辑,我就需要使用它来说明当然):

App.SensitiveDataController = Ember.ObjectController.extend({
  queryParams: ['encryptionKey'],
  encryptionKey: null,

  updateEncryptKey: function() {
    this.set('encryptKey', this.get('encryptionKey'))
  }.observes('encryptionKey')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM