Snewedon
Snewedon

Reputation: 2460

Polymer Adding a behavior to a element

I've created a new Polymer Project.

via terminal mkdir project; cd project; polymer init; Then create a application drawer template.

Whats the correct way to add a behavior to the my-view2 element in src/my-view2.html?

If I create a behaviour at src/some-behavior.html

<script>
SomeBehavior = {
    testing: function()
    { alert('hello'); }
}
</script>

Then importing it in view2

<link rel="import" href="some-behavior.html">

and including it in polymer

Polymer({

  is: 'my-view2',

  behaviors: [SomeBehavior]

});

and adding a button to the the html to invoke the new function i've just created.

<button on-tap="testing">Test</button>

Then building the polymer application via

sudo polymer build # im using sudo because my permissions are wrong

Then deploying the build/bundled directory to firebase with

sudo firebase deploy # same dodgy permission problem

This will work on Desktop: I've checked with Chrome, Firefox and Safari all with the most uptodate version.

But on iOS I run into a problem with the view2 page not loading. I've tested on iPhone osx 9.3 (I think, most uptodate version anyway) then in a emulator with iPhone 4s, 5s, 6s

If I remove the behavior from the page, rebuild and deploy, it goes back to working.

If I run a polymer lint I get this warning

Behavior SomeBehavior not found when mixing properties into my-view2!

I'm I doing anything wrong?

Upvotes: 0

Views: 981

Answers (1)

Casey Carroll
Casey Carroll

Reputation: 1

According to the Polymer Project Documentation, you'll need to define your own namespace in your behavior html file like so:

window.MyBehaviors = window.MyBehaviors || {};
MyBehaviors.SomeBehavior = { ... }

This adds a MyBehaviors namespace to the global window object, so the behavior can be referenced from your element as "myBehaviors.SomeBehavior".

I believe Polymer requires this to avoid collisions with current and future Polymer behaviors.

Also see Rob Dodson's video on Behaviors.

Upvotes: 0

Related Questions