0

I am new to typescript and i am using it for my asp net mvc project. I am using it to render dynamic stuff and to well structure the code. I am currently binding a model to a view and rendering data using jquery event. I am just wondering should I divide my ts file into multiple file or have a big one ?

Here is an example

//here is my class
class Student {
}

//here is the viewmodel bind to the razor view
class StudentViewModel {
}

//the stuff call to execute my actions
var sm = new StudentViewModel
//some stuff and some jquery event binding
ko.applyBindings(sm)

Should I put each part in one ts file or just have a big ts file ? And should i put the jquery event in a ts file( I am using function implemented in ts class) ?

1 Answer 1

2

Brief: I find multiple files and keeping event triggered javascript methods in the main controller ts to be the easiest to follow and maintain. Others will have different opinions as they should. There is no concrete correct way to do it, the benefit i find of having multiple ts files means i can bind them into an index and make them re-usable in other areas of my project, i also find it easier to update and find what im looking for by dividing up the code into manageable files.

N.B: I think this works especially well when you are using a micro-services based architecture.

Longer answer:

I would suggest looking into the structures and layouts for angular through google and in many of the tutorials or examples like so:

https://medium.com/opinionated-angularjs/scalable-code-organization-in-angularjs-9f01b594bf06

In brief, i find that angular offers bound events within the html code that provide a fair if not justified reason to maintain your events withing the typescript controller files. These are the files which are set to communicate directly with the html and handle the front end, therefore it makes sense to either store these in a javascript file called in the html, or move these into the typescript file.

As for your model and view model layout that depends on the architecture youre going for, id suggest looking into MVVM and standard angular set ups for architecture.

Please dont feel my answer is concrete or correct, coding and architecture varies depending on the organisation and its aims and products. No one architecture works out the box for everyone. My opinions are also subject to change over time, something thats thought to be a good standard now could change or be different at any point. My best suggestion is very simply (the laziest answer too, sorry) that you should check best practices and architecture from angular's website or other up to date tutorials.

good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the answer, very intersting and helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.