framework icon indicating copy to clipboard operation
framework copied to clipboard

repeat.for renders same item twice

Open powerbuoy opened this issue 9 years ago • 70 comments

I have a strange issue where when I try to push an empty object to an array that is rendered in my view - the newly pushed element is rendered twice.

The duplicate element is in fact the exact same element as the previous one (if I change the first the second also changes).

I'm unable to reproduce this in a GistRun (perhaps version differences) - but I have managed to reproduce it in a completely new Aurelia CLI project with the following code:

app.js

export class App {
	constructor () {
		this.items = [];
		this.editing = false;
	}

	// Toggle the edit form
	toggleEditing () {
		this.editing = !this.editing;

		// Also add a new item
		if (this.editing) {
			this.items.push({}); // NOTE: This is where things go wrong
		}
	}
}

And this view:

app.html

<template>

	<template if.bind="!editing">
		<h2>${items.length} items!</h2>
		<ul>
			<li repeat.for="item of items">
				${item.name}
			</li>
		</ul>
		<button click.delegate="toggleEditing()">Edit</button>
	</template>

	<template if.bind="editing">
		<ul>
			<li repeat.for="item of items">
				<input type="text" value.bind="item.name">
			</li>
		</ul>
		<button click.delegate="toggleEditing()">Save</button>
	</template>

</template>

My package.json looks like this (for version info):

package.json

{
  "name": "autest",
  "description": "An Aurelia client application.",
  "version": "0.1.0",
  "repository": {
    "type": "???",
    "url": "???"
  },
  "license": "MIT",
  "dependencies": {
    "aurelia-bootstrapper": "^2.1.1",
    "aurelia-animator-css": "^1.0.2",
    "bluebird": "^3.4.1",
    "requirejs": "^2.3.2",
    "text": "github:requirejs/text#latest"
  },
  "peerDependencies": {},
  "devDependencies": {
    "aurelia-cli": "^0.31.3",
    "aurelia-testing": "^1.0.0-beta.3.0.1",
    "aurelia-tools": "^1.0.0",
    "gulp": "github:gulpjs/gulp#4.0",
    "minimatch": "^3.0.2",
    "through2": "^2.0.1",
    "uglify-js": "^3.0.19",
    "vinyl-fs": "^2.4.3",
    "babel-eslint": "^6.0.4",
    "babel-plugin-syntax-flow": "^6.8.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-es2015-modules-amd": "^6.8.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.10.3",
    "babel-plugin-transform-flow-strip-types": "^6.8.0",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-stage-1": "^6.5.0",
    "babel-polyfill": "^6.9.1",
    "babel-register": "^6.24.0",
    "gulp-babel": "^6.1.2",
    "gulp-eslint": "^2.0.0",
    "gulp-htmlmin": "^3.0.0",
    "html-minifier": "^3.2.3",
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.22",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-babel-preprocessor": "^6.0.1",
    "browser-sync": "^2.13.0",
    "connect-history-api-fallback": "^1.2.0",
    "debounce": "^1.0.2",
    "gulp-changed-in-place": "^2.0.3",
    "gulp-plumber": "^1.1.0",
    "gulp-rename": "^1.2.2",
    "gulp-sourcemaps": "^2.0.0-alpha",
    "gulp-notify": "^2.2.0",
    "gulp-watch": "^4.3.11"
  }
}

Maybe it's something obvious I've missed? But it seems like a genuine bug to me.

I'm attaching my example app as well which exhibits the bug and can be run in Firefox directly. Just click "Edit" and two input fields will be shown instead of one - changing one actually changes both.

autest.zip

powerbuoy avatar Oct 06 '17 11:10 powerbuoy

If you are using [email protected], possibly it's related to aurelia/templating-resources#317

bigopon avatar Oct 07 '17 12:10 bigopon

@bigopon I am indeed using 1.5.1. I'm not sure they're related though?

I've simplified my example code slightly. And FYI I'm using Aurelia CLI 0.31.3 (which I believe is the latest version?)

powerbuoy avatar Oct 09 '17 12:10 powerbuoy

Do you get the same issue if you remove the if-part from the example?

jwx avatar Oct 09 '17 13:10 jwx

@jwx I do not. Removing the if solves the issue. Also switching to show instead of if solves it - however, show does not appear to work with <template>.

Edit: I guess it might be the exact same issue then?

powerbuoy avatar Oct 09 '17 13:10 powerbuoy

@powerbuoy Then the problem is probably with if, so yeah, at least related to it. There's a fix in that one that you could try and see if it helps.

jwx avatar Oct 09 '17 14:10 jwx

Ok, honestly I'm not sure how I would implement the fix? You mean the ifCore override right?

I went ahead and updated all Aurelia packages today (I basically deleted node_modules and just reinstalled everything with my package.json pointing to the latest versions of all packages). I've also got the latest version of node and npm.

However, I still get the same issue as above and now new issues are introduced that also seem related to if.bind. When toggling the visibility of a <template> things (bindings) simply stop working. My app is littered with this code so it isn't really feasible for me to change it all now.

I'm guessing this is the same issue as mentioned in https://github.com/aurelia/templating-resources/issues/317 and https://github.com/aurelia/templating-resources/issues/315

How does one go about rolling back to a previous version? Most of the Aurelia packages aren't in package.json so not sure how their version numbers are controlled?

powerbuoy avatar Oct 11 '17 10:10 powerbuoy

How does one go about rolling back to a previous version? Most of the Aurelia packages aren't in package.json so not sure how their version numbers are controlled?

I've spent hours trying to rollback to a previous version with JSPM. Without luck. Problem was, that I'm not able to LOCK a specific version of templating-resources and it's gonna overridden by the dependency tree: aurelia-materialize-bridge -> aurelia-framework -> aurelia-templating -> aurelia-templating-resources.

If you figured out how to rollback, tell me, I will really appreciate that!

mreiche avatar Oct 11 '17 11:10 mreiche

I've pretty much swapped out all my if.bind:s to show.bind:s instead and while it meant I had to re-code some stuff that will require rigorous testing it seems to be working at first glance for now at least.

Here's hoping for an official patch soon :)

powerbuoy avatar Oct 11 '17 12:10 powerbuoy

I've pretty much swapped out all my if.bind:s to show.bind:s instead and while it meant I had to re-code some stuff that will require rigorous testing it seems to be working at first glance for now at least.

Yes, but that doesn't help for repeat.

mreiche avatar Oct 11 '17 12:10 mreiche

I see there's a fix for both https://github.com/aurelia/templating-resources/issues/317 and https://github.com/aurelia/templating-resources/issues/315 out. However, my repeat.for issue, as well as the issues I have with if.bind are not solved by the new release.

I managed to reproduce my issue in gist.run now as well; https://gist.run/?id=af8b192abd4ce89a62fe1383dba79d93

Changing from if.bind to show.bind solves it there too. I'm not 100% sure that gist is using the latest version of everything, but this same thing happens locally with everything updated ([email protected])

powerbuoy avatar Oct 19 '17 17:10 powerbuoy

@powerbuoy Not released yet.

StrahilKazlachev avatar Oct 19 '17 17:10 StrahilKazlachev

Ah ok, my bad :) I guess the fix is in templating-resources?

powerbuoy avatar Oct 19 '17 17:10 powerbuoy

Yes.

StrahilKazlachev avatar Oct 19 '17 17:10 StrahilKazlachev

This is happening to me, but has nothing to do with if or show:

The result is that the content is rendered twice for each array item that is added using myArray.push(newItem) after the view has been bound.

What is really weird is that when I do a production build, the bug goes away.

<template>
<div repeat.for='item of myArray'>
    Item
</div> 
</template>

I am on aurelia-templating-resources v1.5.3

dkent600 avatar Oct 24 '17 16:10 dkent600

OK, referring to my repro case just above ^^^, I figured out how to make the bug go away, even in my dev build. First, note that this applies to aurelia-typescript-webpack. The change is in webpack.config.js.

The original, that works, looks like this in the entry property:

entry: {
    app: ['aurelia-bootstrapper'],
    vendor: ['bluebird', 'jquery', 'bootstrap'],
  }

What doesn't work, and causes this bug to happen in the development build, is the following:

    entry: {
      app: ['aurelia-bootstrapper'],
      vendor: [
        'bluebird', 
        'aurelia-templating', 
        'aurelia-binding', 
        'aurelia-router', 
        'aurelia-templating-binding', 
        'aurelia-polyfills',
        'aurelia-event-aggregator',
        'bootstrap', 
        'jquery',
        ],
  }

dkent600 avatar Oct 24 '17 20:10 dkent600

I'm still seeing this issue in my app, using [email protected]. This bug is causing me to have to jump through all kinds of hoops to avoid.

esmoore68 avatar Nov 08 '17 22:11 esmoore68

@esmoore68 does it occur with [email protected] ?

@jods4 If it's ok with 1.4.0, then i think repeat has some issue, not the if

bigopon avatar Nov 08 '17 22:11 bigopon

I just tried a CLI build with no problems - [email protected]. @esmoore68 please provide a repro of the problem you are facing.

StrahilKazlachev avatar Nov 08 '17 22:11 StrahilKazlachev

@StrahilKazlachev You might be able to repro with a webpack.config such as I described above.

dkent600 avatar Nov 09 '17 00:11 dkent600

@StrahilKazlachev, the actual cases (there are many) in my app might be difficult to distill, but I can easily recreate using the test case created by @powerbuoy above.

@bigopon- It still happens with [email protected].

Aurelia entries from my packages.json:

    "aurelia-animator-css": "^1.0.1",
    "aurelia-binding": "^1.5.0",
    "aurelia-bootstrapper": "^2.1.1",
    "aurelia-dialog": "^1.0.0-rc.1.0.3",
    "aurelia-event-aggregator": "^1.0.1",
    "aurelia-fetch-client": "^1.1.1",
    "aurelia-http-client": "^1.0.4",
    "aurelia-templating": "^1.6.0",
    "aurelia-templating-resources": "^1.4.0",
    "aurelia-validation": "^1.0.0",

I am using Typescript and Webpack.

related settings from my webpack.config:

const {AureliaPlugin, ModuleDependenciesPlugin } = require("aurelia-webpack-plugin");

module.exports = {
  entry: "aurelia-bootstrapper",
  },

esmoore68 avatar Nov 09 '17 14:11 esmoore68