Reputation: 47
I want to create a supplier model i.e code, name, address, phone
I am also going to use the aurelia-validation classes.
Using them both together with the viewmodel - SupplierMaint.js & view - SupplierMaint.html is fine but I want to keep the Supplier model in another file so that I can use it in many places to ensure a consistent validation of values lengths etc.
How should I go about this?
I suspect the @NoView annotation might have something to do with it but I am not sure how to import the external file and wire it up to the submit & validation processing in the SupplierMaint.js
Bob
Upvotes: 0
Views: 200
Reputation: 14995
This is how I do it -
export class Supplier {
name = '';
modelId = '';
constructor(data) {
Object.assign(this, data);
}
}
This let's anyone consume your model and create a new instance of it. The data that is passed in will override the default values you define, but if they do not you still have those default values.
Validation is currently in a state of flux. I'm hoping to be done with the initial refactoring work by the end of this weekend but here is a preview for what I propose as the best usage, feedback welcome.
import {ValidationReporter} from 'aurelia-validate';
export class PersonViewModel {
activePerson;
static inject = [ValidationReporter];
constructor(reporter) {
this.activePerson = new PersonModel({firstName: 'Lucky Luke'});
this.reporter = reporter.subscribe(validationErrors => {
// do something with validationErrors
});
}
}
class PersonModel {
@length({ minimum: 5, maximum: 25 }) firstName = 'Luke';
@required lastName = 'Skywalker';
@date lastUpdated = new Date();
@datetime lastTimeUpdated = new Date();
@email email = '[email protected]';
@length({ minimum: 5, maximum: 25 }) password = 'equal';
@equality confirmPassword = 'equal';
@url website = 'http://www.google.com';
@numericality friendCount = 25;
@numericality({ noStrings: true }) age = 25;
constructor(data) {
Object.assign(this, data);
}
}
Upvotes: 1