1

I started using Firebase (AngularFire) for synchronizing my data for my application. It's a Card tool for Scrum that adds cards to an array. You can manipulate the input fields.

In the first place I used localStorage, which worked really well. Now that I basically implemented Firebase, I got the following problem: After typing a single key into one field, the application stops and the only way of resuming typing is to click in the input field again.

Do you know why this is? Thank you very much in advance!

That's my basic implementation in my Controller:

Card = (@color, @customer, @points, @number, @projectName, @story) ->

        $scope.cards = []
        reference = new Firebase("https://MYACCOUNT.firebaseio.com/list")
        angularFire(reference, $scope, "cards")

        $scope.reset = ->
            $scope.cards = []

        $scope.addCardRed = (customer) ->
            $scope.cards.push new Card("red", customer)

That's my Markup:

<div class="card card-{{ card.color }}">
    <header>
        <input class="points" contenteditable ng-model="card.points"></input>
        <input class="number" placeholder="#" contenteditable ng-model="card.number"></input>
        <input class="customerName"  contenteditable ng-model="card.customer.name"></input>
        <input class="projectName" placeholder="Projekt" contenteditable ng-model="card.projectName"></input>
    </header>
    <article>
        <input class="task" placeholder="Titel" contenteditable ng-model="card.task"></input>
        <textarea class="story" placeholder="Story" contenteditable ng-model="card.story"></textarea>
    </article>
    <footer>
        <div class="divisions">
            <p class="division"></p>
            <button ng-click="deleteCard()" class="delete">X</button>
        </div>
    </footer>
</div>
<div class="card card-{{ card.color }} backside">
    <article>
        <h2 class="requirement">Requirements</h2>
        <textarea class="requirements" placeholder="Aspects" contenteditable ng-model="card.requirements"></textarea>
    </article>
</div>
christophe
  • 692
  • 4
  • 14
  • 27
  • Is this markup contained within an directive? If not, where is $scope.card specified? – bennlich Oct 20 '13 at 15:12
  • 1
    p.s. my first guess is that this is actually a duplicate of http://stackoverflow.com/questions/15119086/angular-js-cant-edit-dynamically-created-input-fields (and perhaps surprisingly unrelated to Firebase/AngularFire!) – bennlich Oct 20 '13 at 15:13
  • yeah, it is within a ng-repeat "card in cards". – christophe Oct 20 '13 at 20:28
  • Please get the latest version of angularFire from Github: https://github.com/firebase/angularFire - it fixes the issue. – Anant Oct 21 '13 at 21:59
  • Anant, I had the issue using the latest version. Can you elaborate? – Adam Grant Oct 22 '13 at 02:06

2 Answers2

2

I ran into this as well. This is because it's recalculating the entire array. Here's how I fixed it:

Bind your input to an ng-model and also add this focus directive

<input class="list-group-item" type="text" ng-model="device.name" ng-change="update(device, $index)" ng-click="update(device, $index)" ng-repeat='device in devices' focus="{{$index == selectedDevice.index}}" />

I set the selectedDevice like this

$scope.update = function(device, index) {
  $scope.selectedDevice = device
  $scope.selectedDevice.index = index
}

Now create this directive.

angular.module('eio').directive("focus", function() {
  return function(scope, element, attrs) {
    return attrs.$observe("focus", function(newValue) {
      return newValue === "true" && element[0].focus();
    });
  };
});

Update Sorry for the delay, had a few things to tend to.

The reason why this works is because it is constantly saving the index value of the item in the array you are currently selecting. Once focus is lost, focus is returned immediately by going to that index.

If we're talking about multiple arrays, however, you'll need to refactor the setSelected code to say which array it is.

So you'd want to change

focus="{{$index == selectedDevice.index}}"

to something like

focus="{{$index == selectedDevice.index && selectedDevice.kind == 'points'}}"

Where points is the category of the array where the code appears.

Adam Grant
  • 12,477
  • 10
  • 58
  • 65
  • 1
    Obviously, where I have ``device`` you want ``card`` :) – Adam Grant Oct 20 '13 at 20:15
  • Hey! Thank you so much for this solution! It works for one input, but not for all! How can I adjust this code to work in every input field I got? I will post my current markup at the bottom. Would be great, if you could answer this question! :) – christophe Oct 20 '13 at 20:55
  • can anyone still answer this question, please? – christophe Oct 22 '13 at 12:46
  • Thank you for your effort, @ajkochanowicz! Could you please explain it a bit more regarding my different card properties? I just don't get the new line of code. How do I have to adjust my update method and my directive? Thank you again for your time and help! – christophe Oct 24 '13 at 20:12
  • 2
    This problem will actually go away with the newest version of AngularFire. – Adam Grant Nov 05 '13 at 02:44
  • I take that back. The newest version of AngularFire does not necessarily solve anything. – Adam Grant Nov 20 '13 at 22:18
  • I had the same problem :) You solution save me. The current version of AngularFire (1.2) still doesn't introduce the fix :( – Przemek Nowak May 03 '16 at 13:34
1

I sorted this one by downloading the most recent version of angularFire.js, seems like bower installed the on that didn't have this fix. now my contentEditable is!

irth
  • 1,696
  • 2
  • 15
  • 24