-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin instance is ignoring new path function set with InfiniteScroll.prototype.option method #936
Comments
Thanks for reporting this issue! You are correct, the path behavior will not be changed after you re-set // set new path option
infScroll.option.path = 'new path';
// update behavior
infScroll.updateGetPath(); Since your demo is already using a function, another solution is to update variables used within the function, so the same function could be used. See CodePen |
So what you are saying is I basically need a global variable, which will work in this case for sure. But what if instance initialization is done in a separate file inside a function where I have no way to access anything global. I can «hack» it with object reference and а function that accepts options object. // this lives in some initscroll.js
export default function initiInfScroll(options) {
return new InfiniteScroll(".js-container", {
path() {
return options.pathUrl;
}
});
}
// app.js
import initiInfScroll from 'path/to/initscroll';
const infScrollOptions = {
pathUrl: 'https://jsonplaceholder.typicode.com/todos/1'
}
const infScroll = initiInfScroll(infScrollOptions);
document.querySelector(".js-change-path-btn").addEventListener("click", () => {
infScrollOptions.pathUrl = "https://jsonplaceholder.typicode.com/todos/2";
}); But this just feels so wrong. Mutating cross-module object reference is not exactly safe. Actually doing some monkey patching feels better and kinda ok, given the situation. // Adding pathUrl field to this instance, so there will be no prototype pollution
// and then using it on the instance
const instance = new InfiniteScroll(".js-container", {
path() {
return this.pathUrl ?? '/1'
},
})
// And then just change it when needed
instance.pathUrl = '/2' Are there any plans in the API to support this case (which is very common to be honest) or monkey patching is the way? |
In this case, I recommend you trigger |
Will try and get back to you with results. |
Hi. Got same issue, and add another problem about currentIndex My index have form, loading content thrue Ajax. I update path like this : $(document).on('loaded.index.app', (event, body) => {
if (body.path) {
console.log("new path=" + body.path);
this.infinite.options.path = body.path;
this.infinite.updateGetPath();
}
}) Here the process first load, Initialize infinite-scroll
Add
Scroll down
Scroll up, change query search to
getPath and index are not reset. |
What it says in the description. My case is a form where a user can enter a search term for an api request. Every new form submission I need to reset path option to generate proper URL with query param.
Made a simple live example where after infScroll.option({path() {}}) the instance is still using the path function that it was initialized with. Here's a link to CodePen.
PS: Under the hood option method is using Object.assign and it actually works fine with other options, but for some reason ignores new path function.
PSS: Perhaps I'm supposed to destroy and reinitialize a new instance on every form submission?
The text was updated successfully, but these errors were encountered: