Ember数据变换中的访问控制器属性 [英] Access controller properties within Ember Data Transform
问题描述
也许对我的用例几句话。我喜欢创建一个自定义属性encryptedStrings,并使用DS.Transform通过使用Stanford Javascript Crypto Library使用给定的键对字符串进行加密/解密。关键应该是uri中的一个查询参数。
get方法在DS.Transform中定义,但我没有进一步。这里代码的相关部分:
App.EncryptedStringTransform = DS.Transform.extend({
deserialize:function (序列化){
var key = this.get('pollController.encryptionKey');
返回Ember.isNone(序列化)?null:String(sjcl.decrypt(key,serialized));
$,
serialize:function(deserialized){
var key = this.get('pollController.encryptionKey');
返回Ember.isNone(反序列化)?null:String(sjcl .encrypt(key,deserialized));
}
});
App.PollController = Ember.ObjectController.extend({
queryParams:['encryptionKey'],
encryptionKey:'default'
});
而不是
code> this.get( 'pollController.encryptionKey');
我也尝试过这个:
console.log(this.get('controller.encryptionKey'));
console.log(this.get('controllers.poll.encryptionKey'));
console.log(this.get('controllers.pollController.encryptionKey'));
有趣!我想知道将加密密钥如何暴露在URL中是个好主意。但是,假设这是你想要做的,而不是将数据转换为序列化程序的一部分,我想我将做的是将键设置为模型上的属性(从queryParam所在的路由)。然后,将基于密钥和加密值导出的模型中的未加密值计算为属性,这些值将只是常规的 DS.attr()
。计算属性可以是getter和setter ,所以如果在未加密的属性上设置一个新值,然后您可以设置重新加密的值。
更新:
下面是一个如何以可重复使用的方式编写计算属性的示例:
Ember。 calculate.encrypted = function(encryptedField,keyField){
返回Ember.computed(encryptedField,keyField,function(key,decryptptedValue){
var encryptKey = this.get(keyField),encryptedValue;
// setter
if(arguments.length == 2){
encryptedValue = Ember.isNone(decryptptedValue)?null:String(sjcl.encrypt(encryptKey,decryptptedValue));
this .set(encryptedField,encryptedValue);
}
encryptedValue = this.get(encryptedField);
返回Ember.isNone(encryptedValue)?null: String(sjcl.decrypt(encryptKey,encryptedValue));
});
};
然后,您可以这样写你的模型:
App.SensitiveData = DS.Model.extend({
encryptKey:DS.attr('string'),
encryptedField1:DS.attr string'),
encryptedField2:DS.attr('string'),
decryptptedField1:Ember.computed.encrypted('encryptedField1','encryptKey'),
decryptptedField2:Ember.computed。加密('encryptedField2','encryptKey')
});
更新2:
您可以通过路由设置模型上的加密密钥,如下所示:
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;
});
}
});
或者,假设数据不通过路由暴露,而只能通过控制器,您可以添加一个观察者到控制器,并通过观察者设置值,像这样(注意,这是不可能的,这甚至不是必要的,并且由于控制器代理到模型,只要模型中的属性与查询参数名称匹配在控制器中指定,你甚至不需要做观察者逻辑,我需要玩弄它来肯定地说):
App.SensitiveDataController = Ember.ObjectController.extend({
queryParams:['encryptionKey'],
encryptionKey:null,
updateEncryptKey:function() {
this.set('encryptKey',this.get('encryptionKey'))
} .observes('encryptionKey')
});
I like to access a controller property within 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. The key should by part a query param in the uri.
get method is defined in DS.Transform but I did not get further. 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. 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). 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()
. 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.
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:
You can set the encryption key on your model via the Route, like this:
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')
});
这篇关于Ember数据变换中的访问控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!