allegro / handlebars-spring-boot-starter

Spring Boot auto-configuration for Handlebars
Apache License 2.0
108 stars 26 forks source link

Reload appends same data #19

Closed apps4u closed 8 years ago

apps4u commented 8 years ago

HI, Ive only just started using this package.

I'm using SpringBoot project and I created a test template which I'm passing a list of people Objects to the People Class has two properties firstName lastName now when I run the app the array of people a displayed on the page using the {{#each }} helper .

I've got 2 people objects so I get 3 row on the page when I reload the page I appends to the list the same 2 people again so each refresh is not reloading the page.

<!DOCTYPE html>
<head>

    <title>Index</title>
    <script src="/webjars/jquery/2.2.4/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="/webjars/bootstrap-material-design/0.5.9/js/material.min.js"></script>
    <script src="/webjars/bootstrap-material-design/0.5.9/js/ripples.min.js"></script>

    <link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="/webjars/bootstrap-material-design/0.5.9/css/bootstrap-material-design.min.css">
    <link rel="stylesheet" href="/webjars/bootstrap-material-design/0.5.9/css/ripples.min.css">
</head>
<body>
<div class="container">
    <p>HB reload</p>

    {{#each people}}
        <p>{{firstName}} {{lastName}}</p>
    {{/each}}
<p>------------------------------------------</p>
</div>
</body>
</html>

after reloading the page a few times this is what is displayed

HB

jason kristian

jhon other

jason kristian

jhon other

jason kristian

jhon other

jason kristian

jhon other

jason kristian

jhon other

jason kristian

jhon other


adamdubiel commented 8 years ago

This looks like you have some state in one of your beans (some PeopleService or controller?) And you append the same data to this collection/cache, which is later passed to templating engine.

apps4u commented 8 years ago

I was just testing templates so the controller method creates the array or people to pass to the view so that would run for each request.

@Controller
public class IndexController {

    List<String> names = new ArrayList(Arrays.asList("jason", "peter", "monike", "eva"));

    class Person {
       public String firstName;
      public  String lastName;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
    List<Person> people = new ArrayList<Person>();

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        Person p = new Person();
        p.firstName = "jason";
        p.lastName = "kristian";

        Person p1 = new Person();
        p1.firstName = "jhon";
        p1.lastName = "other";

        people.add(p);
        people.add(p1);

        model.addAttribute("people", people);
        model.addAttribute("names", names);

        return "index";
    }
}
apps4u commented 8 years ago

Sorry your Right I dont know how I missed it the List is declared out side of the request method and each run through I add to the list.

Im so sorry , I been looking at the same piece of code for that issue and I did not see it.

So please accept my apology.

adamdubiel commented 8 years ago

It's fine, glad to be helpful as a debugger ;)