Polymer - gpawade/gpawade.github.io GitHub Wiki

Polymer Help

Properties

properties : {

	//Property inline declaration
	name : String,

	// Property declaration
	propName:{

		// Type - String, Number, Boolean
		type: String,

		// Set Default value
		value : "some text",

		// Reflect property to  host attribute when changed
		reflectToAttribute : true,

		// Property set readonly, if true, can't assigne or bind
		readOnly : true,

		//if true, property is available for two-way data binding. Event, 'property-name-changed' is fire whenever property changed
		notify: true,


		observer: "method_name"

	}
}

Behaviours

Extending custom element prototypes

// Declaration in Polymer
behaviors: [SuperBehavior]

Behaviour Declaration

<script>
	SuperBehavior = {

		properties:{

		},

		methodName(){

		}
	}
</script>

Listner

listeners:{

	// Host Event
	"tap" : "_onHostClick",

	// Control Event
	"btnSave.tap": "_onButtonClick",

	// Event fired
	"event-fired": "_onEventFire"
}


// Fire event
this.fire("event-fired", arg);

Observers and compouted Property

Observers are methods invoked when observers changes occured.

Observe properties

properties  :{

	disabled : {
		type : Boolean,
		observe : '_disabledChanged'
	}
},

_disabledChanged : function(newValue, oldValue){
	// do work when disabled property change
}

Complex Observer

Complex observers are declared in the observers array, and can monitor one or more paths.

observers:[
	'_userListChanged(user.*, filter)'
]

Note - The initial call to a complex observer is deferred until all of the dependencies are defined (that is, they don't have the value undefined).

Array

//property
 item = [{name : "jon"}, { name : 'doe'}]

 // Get Item from Array
 this.get("item.0")


 //set array item
 this.set("item.0.", 'some text');

Custome Style

update the component dynamically with custome variable

this.customStyle['--my-elem-visibility'] = 'visible';
this.updateStyles()

Test

Polymer use web-component-tester for testing.

web-component-tester included below library out of box -

  • Mocha as a test framework.
  • Chai assertions.
  • Async to keep your sanity.
  • Lodash (3.0) to repeat fewer things.
  • Sinon and sinon-chai to test just your things.
  • test-fixture for easy fixture testing with
  • accessibility-developer-tools through a11ySuite for simple, automated Accessibility testing.

Run Test

$ polymer test

OR

$ wct

$ wct -p    // keep open the browser after test run

https://medium.com/google-developer-experts/polymer-testing-tips-f217ba94a64

http://t-code.pl/blog/2015/08/polymer-templatizer/

⚠️ **GitHub.com Fallback** ⚠️