0

I'm having problems figuring out how polymer data binding to an elements host works. I would like to change the selected page of a <neon-animated-pages> element depending on the user being logged in or not but I'm totally new to polymer and have no idea how to do this with data bindings. I can't even get them to change inside of an element with javascript...

Any help would be highly appreciated!

My main element looks something like this:

<template>
  <neon-animated-pages id="animated_pages" selected='{{display}}' entry-animation='slide-from-left-animation' exit-animation='slide-right-animation'>
    <neon-animatable><login-page loggedIn={{loggedIn}} user={{user}}></login-page></neon-animatable>
    <neon-animatable><main-page></main-page></neon-animatable>
    <neon-animatable>Baz Page, the Best One of the Three</neon-animatable>
  </neon-animated-pages>
</template>

Polymer({
  is: 'app-page',

  properties: {
    signedIn: {
      type: Boolean,
      notify: true,
      value: false,
      observer: 'showMain'
    },
    user: {
      type: Object,
      notify: true,
    }
  },
  showMain: function(){
    this.displayed = 1;    <--doesnt seem to work!?!
});

My <login-page> element looks something like this:

<template>
  <firebase-auth
     id="auth"
     app-name="MyApp"
     provider="password"
     signed-in="{{signedIn}}"
     user="{{user}}">
  </firebase-auth>

  <form is="iron-form" id="login_form" class="bottom">
    <div>{{signedIn}}</div>
    <paper-button id="login_button" on-tap="logIn" raised>Log in</paper-button>
    <paper-input id="login_password" name="password" label="password" type="password" on-keyup="_handleEnter" required></paper-input>
    <paper-input id="login_email" ></paper-input>
  </form>
<template>

<script>
  Polymer({

    is: 'login-page',

    properties: {
      signedIn: {
        type: Boolean,
        notify: true,
      },
      user: {
        type: Object,
        notify: true,
      }
    },
  logIn: function() {
      var email = this.$.login_email.value
      var password = this.$.login_password.value
      firebase.auth().signInWithEmailAndPassword(email, password)
      .then(function(response) {
        this.signedIn = true;    <--doesn't seem to work!?!
        this.user = response;
      })
      .catch(function(error) {
        console.log(error)
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        alert(errorMessage);
        this.signedIn = false;
      }.bind(this));
    },
  });
</script>
Raspel
  • 283
  • 4
  • 16

1 Answers1

0

It looks like you're using the wrong property name in your binding:

<login-page loggedIn={{loggedIn}}></login-page>

Note that <login-page> does not have a property named "loggedIn". You probably meant to use "signedIn".

Also note that property bindings use the property name in attribute form (dash-case not camelCase) (see docs on Data Binding). To bind "signedIn", you would use:

<login-page signed-in="{{loggedIn}}"></login-page>

Lastly, you've bound this only in your catch callback but not the one for then, so the latter's this is not referring to your Polymer object. Fix:

Polymer({
  ...
  logIn: function() {
    ...
    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(function(response) {
        this.signedIn = true;
        this.user = response;
      }.bind(this)) // bind `this` to the Polymer object
      .catch(function(error) {
        ...
      }.bind(this));
  })
});
tony19
  • 125,647
  • 18
  • 229
  • 307
  • Thanks for the tips! However i still can't change the value in javascript... Neither `this.user = newUser` nor `this.setAttribute('signed-in`, true)` seems to work. Also i've tried replacing `this` with `document.querySelector('login-page')` which also doesn't seem to have any effect. Am I doing something fundamentally wrong here? – Raspel Oct 18 '16 at 10:12
  • `this` is not bound to your Polymer object in your `then` callback. See updated answer. – tony19 Oct 18 '16 at 10:30