2

I'd like to link text inputs with CSS to recreate the WordPress theme (live) customizer, where changing a value in an input update a specific CSS value.

I created a {{ Ember.TextField }} in my template, and a <style> tag with the correct CSS declaration with the Ember variable in it, to update the CSS. I get an error telling me that Ember can't access the variable in my <style> tag because it's out of the DOM; that makes sense, the generated code is a <script> inside a <style> inside another <script>, a bit ugly.

I'm wondering if there is a (clean) Ember way to link an input/textarea/select field with CSS properties. I read Ember.js - Update CSS width based on an object property, but adding {{bindAttr}} to every tags I'd like to update would probably be counterproductive, so I'm asking Ember experts if there is a way to group all these changes in some way to do what I need.

Otherwise, I guess this will be a jQuery home-made plugin, but I'd prefer to be completely sure to do that before giving up on Ember magic ;)

Thanks!

1 Answer 1

2

I think using an observer in this case would work fine.

App.StyleTextField = Ember.TextField.extend({

  valueDidChange: function() {
    var value = this.get('value');
    var cssText = 'h1 { color: ' + value + '; }'; // You could even use Mustache to generate this if you were so inclined

    // create a <style /> tag with cssText and dump it into the <head />
  }.observes('value')
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.