ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

Problem with changing values VM #200

Closed dnish closed 8 years ago

dnish commented 8 years ago

Hey Manuel, it's me again, I've faced with another problem. I've added a search to our project.

Now this is what happens: Type in a title into the search input and click f.e. on the second item. After that, the conversion should start. Then change your search to another title and click on the new second item again. This doesn't work anymore. You can click on all other items, but not on the second one.

This also happens if you click at the beginning on the first item and after a change of the search title again on the first, etc....

This is the part of my template where I generate the item models:

  {{#if results}}
        <ul>
            {{#each results}}
                {{>search_item}}
            {{/each}}

        </ul>
    {{/if}}

I don't know why this happens, I get no error console message.

ManuelDeLeon commented 8 years ago

This isn't enough. I need a repro. On Mar 5, 2016 11:10 AM, "dnish" notifications@github.com wrote:

Hey Manuel, it's me again, I've faced with another problem. I've added a search to our project, you can check it out here: www.convert.as/search

Now this is what happens: Type in a title into the search input and click f.e. on the second item. After that, the conversion should start. Then change your search to another title and click on the new second item again. This doesn't work anymore. You can click on all other items, but not on the second one.

This also happens if you click at the beginning on the first item and after a change of the search title again on the first, etc....

This is the important part of my template

{{#if results}}

    {{#each results}} {{>search_item}} {{/each}} ```

{{/if}}



I don't know why this happens, I get no error console message.

—
Reply to this email directly or view it on GitHub
https://github.com/ManuelDeLeon/viewmodel/issues/200.
dnish commented 8 years ago

Okay, will try to make a repo. The only thing I now see is that the failed ViewModel still has the same porpertie, f.e. if I do

ViewModel.find("search_item")[0].started()

...is it always true, even if I get new results. The default value for a new VM is false. The funny thing is that wen I do ViewModel.find("search_item")[0].title() I get the correct title....

dnish commented 8 years ago

I'm seeing that on a new search onRendered() isn't executed on any of the new items. Also all ViewModels still have the same id's, even if results change. Do you know any situation where this can happen? On my repro with a light search example this doesn't happen. I'm fetching my results from a Meteor method. This is how I set the results:

Template.search.viewmodel({

results: null,

searchText: null,

onCreated() {
    $("#wrapper").removeClass("toggled");
},

onRendered() {

},

autorun: [
    function () {
        if (this.searchText()) {
            Meteor.call("search", this.searchText(), (err, res) => {

                if (res.hasOwnProperty("items")) this.results(res.items);
            });
        }
    }
]
});

I will now extend my repro.

dnish commented 8 years ago

@ManuelDeLeon Repro is available here: https://github.com/dnish/vm-search-repro

  1. Type in a search title
  2. Click on a item, you should see STARTED on that item.
  3. Change the search title, you will see new results.
  4. STARTED stays on the item position you've clicked on, so the ViewModel wasn't destroyed when you've changed the search. This behavior causes the issue.
dnish commented 8 years ago

It "works" when you add a timeout:

Template.index.viewmodel({

search:null,
results:null,

doSearch() {
   Meteor.call("getResults", (err,res) => {

       this.results(null);
        window.setTimeout(() => {
            this.results(res);
        },5000);
    });
}

});
ManuelDeLeon commented 8 years ago

Your collection needs to have an _id field.

This isn't a ViewModel issue. You'll get the same behavior in Blaze or React.

dnish commented 8 years ago

Ah thanks, I didn't know that.