I have been tasked with creating a vanilla js mvc web app. I have never done this before and have limited knowledge of how things ought to be done (best practices and the like). I would like some advice as to how data/information can be passed between controllers. It would be good if I can use the Observer pattern (I have watched lots of content and explanation on this and I understand the basic - but can I implement it on my own 100 percent? No.). I have browsed previous answers here but I can not understand them yet.
Let me give you an example of my code thus far. Within GeneralModel.js which is used to just do AJAX and display items:
export default class GeneralModel{
url = 'webshop_clothes.csv';
constructor(){
}
sortValues = {
"Id": "id",
"Product name": "product_name",
"Price usd": "price_usd",
"Size": "size",
}
loadData = () =>{
return fetch(this.url)
.then(data=>data.text())
.then(dataAsText=>{
let result = dataAsText.split('\r\n');
let properties = result.shift().split(',');
this.data = result.map(row=>row.split(',').reduce((acc,item,i)=>{
acc[properties[i]] = item;
return acc;
}, {}));
return this.data;
});
}
loadProperties = () =>{
return fetch(this.url)
.then(data=>data.text())
.then(dataAsText=>{
let result = dataAsText
.split('\r\n');
[,...this.properties] = result
.shift()
.split(',');
return this.properties;
});
}
loadProperties is used within my SortingController to generate the fields for sorting - based on what the sorting should be done. Code within SortingController:
import GeneralModel from "../general-output/general-model.js";
import SortView from "../sorting-output/sorting-view.js";
export default class SortController{
constructor(pub){
this.model = new GeneralModel();
this.view = new SortView(this.onClick);
this.pub = pub;
this.model.loadProperties()
.then(data=>{
this.view.render(data);
});
}
onClick = (ev)=>{
this.pub.notifySubscribers(this.pub.events.SORT_ITEMS_BY, ev.target.innerText)
}
}
I am being told that loading the General Model within my Sorting Controller is not a good idea. I thought I would reuse it. This however means I am doing two AJAX requests when just one can be done. I also have a Publisher class which tries to implement the observer pattern. I can amend the post and add it or I can just add the whole repository if that is better.
I would like to know what is a better way to share/send information between Controllers if my way is not good? That is my question. Any and all criticism is welcome, I would just like to learn more about MVC. Any resources/books/courses or just plain advice is welcome since I am a noob.