Last active
August 29, 2015 14:12
-
-
Save ukcoderj/ce7123cdabc0ffb28280 to your computer and use it in GitHub Desktop.
Create AngularJs assigments from an array of different javascript properties (can convert from json first)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var printJavascriptArrayOfPropsAsAngularScopeAssignment = function (longArrayOfProperties, useOneTimeBindingSyntax) { | |
/// <summary>Takes a long list of properties, strips the names, | |
/// creates angular.js assignments/ bindings and outputs to console for copy+paste | |
/// | |
/// - If you have an array of Json data, use angular.fromJson(jsonString) or JSON.parse(jsonString) first to get it in the right format | |
/// View the output in your javascript console (e.g. in Chrome) | |
/// Example Input -> var myProps = { myProperty1: 3, MyProperty2: 'Dave' }; | |
/// Example Output | |
/// $scope.myProperty = inputData.myProperty; | |
/// $scope.myProperty2 = inputData.MyProperty2; | |
/// ... | |
/// <p>{{myProperty}}</p> | |
/// <p>{{myProperty2}}</p> | |
/// ... | |
///</summary> | |
/// <param name="longArrayOfProperties" type="Array(of properties)">An array of properties that'd be a faff to convert manually.</param> | |
/// <param name="useOneTimeBindingSyntax" type="Boolean">Angular 1.3+ -> can declare as one time binding for efficiency.</param> | |
/// <returns type="">No return value - outputs to console.</returns> | |
var propertyNames = Object.getOwnPropertyNames(longArrayOfProperties) | |
var outputString = ''; | |
// Create a set of angular assignments for a big set of properties -> output format -> $scope.myProperty = inputData.myProperty; | |
for (var i = 0; i < propertyNames.length; i++) { | |
var thisProp = propertyNames[i]; | |
if (!thisProp) continue; | |
// Create assignment string (lowercase first letter) | |
outputString += '$scope.' + thisProp[0].toLowerCase() + thisProp.slice(1) + ' = inputData.' + thisProp + ';\r\n'; | |
} | |
outputString = outputString + '\r\n\r\n'; | |
// Create a load of bindings for the UI -> output format <p>{{myProperty}}</p> | |
for (var i = 0; i < propertyNames.length; i++) { | |
var thisProp = propertyNames[i]; | |
if (!thisProp) continue; | |
// Create assignment string (lowercase first letter) | |
var bindingChar = (useOneTimeBindingSyntax == true) ? '::' : ''; | |
outputString += '<p>' + thisProp + ': {{' + bindingChar + thisProp[0].toLowerCase() + thisProp.slice(1) + '}}' + '</p>\r\n'; | |
} | |
console.log(outputString); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment