Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add `Element.Layout#toObject` and `Element.Layout.toHash`.

  • Loading branch information...
commit 949dc83fd4533be0077eea5d6c92a78caa0aae98 1 parent 2fd5c9b
Andrew Dupont savetheclocktower authored
Showing with 41 additions and 0 deletions.
  1. +2 −0  CHANGELOG
  2. +39 −0 src/dom/layout.js
2  CHANGELOG
View
@@ -1,3 +1,5 @@
+Add `Element.Layout#toObject` and `Element.Layout.toHash`. (Andrew Dupont)
+
Make `Element.Layout#toCSS` return camelized property names, as expected by `Element.setStyle`. [#1021 state:resolved] (njakobsen, Andrew Dupont)
*1.7_rc1* (April 1, 2010)
39 src/dom/layout.js
View
@@ -339,6 +339,45 @@
},
/**
+ * Element.Layout#toObject([keys...]) -> Object
+ * - keys (String): A space-separated list of keys to include.
+ *
+ * Converts the layout hash to a plain object of key/value pairs,
+ * optionally including only the given keys.
+ *
+ * Keys can be passed into this method as individual arguments _or_
+ * separated by spaces within a string.
+ **/
+ toObject: function() {
+ var args = $A(arguments);
+ var keys = (args.length === 0) ? Element.Layout.PROPERTIES :
+ args.join(' ').split(' ');
+ var obj = {};
+ keys.each( function(key) {
+ // Key needs to be a valid Element.Layout property.
+ if (!Element.Layout.PROPERTIES.include(key)) return;
+ var value = this.get(key);
+ if (value != null) obj[key] = value;
+ });

Here one ",this" is needed to bind the function to Element.Layout scope. Currently this.get(key) gives an error.
I'm wondering if Element.Layout.PROPERTIES.include(key) is needed, since this.get will throw Exception on error, and I don't know which should be the expected behavior here.
Plus Enum.inject will look nice here :)

p.s. Letter today could try to make a patch for this + the units tests

Andrew Dupont Collaborator

That's what I get for copy/pasting code. :-)

Tell me about it :) I have 2-3 errors like this the past week, but this is the good part of github and the open source as whole :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return obj;
+ },
+
+ /**
+ * Element.Layout#toHash([keys...]) -> Hash
+ * - keys (String): A space-separated list of keys to include.
+ *
+ * Converts the layout hash to an ordinary hash of key/value pairs,
+ * optionally including only the given keys.
+ *
+ * Keys can be passed into this method as individual arguments _or_
+ * separated by spaces within a string.
+ **/
+ toHash: function() {
+ var obj = this.toObject.apply(this, arguments);
+ return new Hash(obj);
+ },
+
+ /**
* Element.Layout#toCSS([keys...]) -> Object
* - keys (String): A space-separated list of keys to include.
*
Radoslav Stankov

Here one ",this" is needed to bind the function to Element.Layout scope. Currently this.get(key) gives an error.
I'm wondering if Element.Layout.PROPERTIES.include(key) is needed, since this.get will throw Exception on error, and I don't know which should be the expected behavior here.
Plus Enum.inject will look nice here :)

p.s. Letter today could try to make a patch for this + the units tests

Andrew Dupont

That's what I get for copy/pasting code. :-)

Radoslav Stankov

Tell me about it :) I have 2-3 errors like this the past week, but this is the good part of github and the open source as whole :)

Please sign in to comment.
Something went wrong with that request. Please try again.