Javascript 使用存储库将对象推送到阵列

Javascript 使用存储库将对象推送到阵列,javascript,aurelia,Javascript,Aurelia,我是奥雷莉亚的新手。我浏览了一些例子,看到了这个。 它有一个页面,列出所有汽车(cars.js),并有一个创建按钮,将您重定向到另一个页面,您可以在该页面中输入新车(addCar.js),一旦点击保存,您将被重定向到汽车列表。很简单 我的问题是,新增加的汽车是如何被推上汽车清单的?本例使用存储库将所有交互抽象到api。这些文件是否共享此.cars的相同实例 dataRepository.js @inject(HttpClient) export class DataRepository {

我是奥雷莉亚的新手。我浏览了一些例子,看到了这个。 它有一个页面,列出所有汽车(
cars.js
),并有一个创建按钮,将您重定向到另一个页面,您可以在该页面中输入新车(
addCar.js
),一旦点击保存,您将被重定向到汽车列表。很简单

我的问题是,新增加的汽车是如何被推上汽车清单的?本例使用存储库将所有交互抽象到api。这些文件是否共享此.cars的相同实例

dataRepository.js

@inject(HttpClient)
export class DataRepository {
    constructor(httpClient) {
        this.httpClient = httpClient;
    }

getCars() {
    var promise = new Promise((resolve, reject) => {
        if (!this.cars) {
            this.httpClient.fetch('api/Cars')
            .then(response => response.json())
            .then( data => {
                this.cars = data;
                resolve(this.cars);
            }).catch(err => reject(err));
        }
        else
            resolve(this.cars);
    });
    return promise;
}

    addCar(car) {
        var promise = new Promise((resolve, reject) => {
            this.httpClient.fetch('api/Cars',{
                method: 'POST',
                body: json(car)
            }).then(response => response.json())
            .then(data => {
                this.cars.push(data); // <-- here, how does this pass the related data to cars.js?
                resolve(data);
            }).catch(err=>reject(err));
        });
        return promise;
    }
}
@inject(DataRepository)
export class AddCar {
    constructor(dataRepository) {
        this.car = { carType: "Big" };
        this.dataRepository = dataRepository;
    }

    activate(params, routeConfig, navigationInstruction) {
        this.router = navigationInstruction.router;
    }

    save() {
        this.validation.validate().then(()=>{
            this.dataRepository.addCar(this.car).then(car=> this.router.navigateToRoute('cars'));
        });
    }
}
@inject(DataRepository)
export class Cars {
    constructor(dataRepository) {
        this.dataRepository = dataRepository;
    }

    activate(params, routeConfig, navigationInstruction) {
        this.cars = [];
        this.router = navigationInstruction.router;
        return this.dataRepository.getCars().then(cars => {
            this.cars = cars;
        });
    }

    addCar() {
        this.router.navigateToRoute("addCar");
    }
}
cars.js

@inject(HttpClient)
export class DataRepository {
    constructor(httpClient) {
        this.httpClient = httpClient;
    }

getCars() {
    var promise = new Promise((resolve, reject) => {
        if (!this.cars) {
            this.httpClient.fetch('api/Cars')
            .then(response => response.json())
            .then( data => {
                this.cars = data;
                resolve(this.cars);
            }).catch(err => reject(err));
        }
        else
            resolve(this.cars);
    });
    return promise;
}

    addCar(car) {
        var promise = new Promise((resolve, reject) => {
            this.httpClient.fetch('api/Cars',{
                method: 'POST',
                body: json(car)
            }).then(response => response.json())
            .then(data => {
                this.cars.push(data); // <-- here, how does this pass the related data to cars.js?
                resolve(data);
            }).catch(err=>reject(err));
        });
        return promise;
    }
}
@inject(DataRepository)
export class AddCar {
    constructor(dataRepository) {
        this.car = { carType: "Big" };
        this.dataRepository = dataRepository;
    }

    activate(params, routeConfig, navigationInstruction) {
        this.router = navigationInstruction.router;
    }

    save() {
        this.validation.validate().then(()=>{
            this.dataRepository.addCar(this.car).then(car=> this.router.navigateToRoute('cars'));
        });
    }
}
@inject(DataRepository)
export class Cars {
    constructor(dataRepository) {
        this.dataRepository = dataRepository;
    }

    activate(params, routeConfig, navigationInstruction) {
        this.cars = [];
        this.router = navigationInstruction.router;
        return this.dataRepository.getCars().then(cars => {
            this.cars = cars;
        });
    }

    addCar() {
        this.router.navigateToRoute("addCar");
    }
}

在不太了解Aurelia的细节的情况下,我假设它使用单实例依赖项注入(因此使用@inject指令)。该实例具有cars集合,当传递给依赖项的构造函数时,该实例被设置为实例成员。可以找到更多信息。

在不太了解Aurelia细节的情况下,我假设它使用单实例依赖项注入(因此使用@inject指令)。该实例具有cars集合,当传递给依赖项的构造函数时,该实例被设置为实例成员。可以找到更多信息。

是的。回答得好。你能把这个问题改一下吗?快问。如果数据存储库包含汽车列表。然后当它被注射的时候。你会不会像这样调用汽车列表
this.dataRepository.cars
?我想最终是的(取决于实现)。存储库模式的要点是,您不一定知道数据的底层结构。它可能是一个列表,但也可能是一个数据库。对于数据库,您不会将其作为属性引用。我建议创建要公开的操作的接口,不要直接公开集合。是的。回答得好。你能把这个问题改一下吗?快问。如果数据存储库包含汽车列表。然后当它被注射的时候。你会不会像这样调用汽车列表
this.dataRepository.cars
?我想最终是的(取决于实现)。存储库模式的要点是,您不一定知道数据的底层结构。它可能是一个列表,但也可能是一个数据库。对于数据库,您不会将其作为属性引用。我建议您创建要公开的操作的接口,而不是直接公开集合。