Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
How does passing data/information from one controller to another work (js mvc)

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.

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!