Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

javascript - Developing an AngularJS app with dynamic set of modules

I have an application with a complex layout where the user could put (drag/drop) widgets (by choosing from a predefined set of 100+ widgets) where every widget is a custom implementation that displays a set of data (fetched using REST call) in a specific way. I've read tons of blog posts, stackoverflow questions and the official AngularJS docs but I cannot figure out how should I design my application to handle there requirements. Looking at demo apps, there is a single module (ng-app) and when constructing it in the .js file the dependent modules are declared as its dependencies, however i have a big set of widgets and somehow it's not advisable to describe them all there. I need suggession for the following questions:

  • How should I design my app and widgets - should i have a separate AngularJS module or each widget should be a directive to the main module?
  • If I design my widget as directives, is there a way to define dependency within a directive. I.e. to say that my directive uses ng-calender in its implementation?
  • If I design each widget as a separate module, is there a way to dynamically add the widget module as a dependency to the main module?
  • How should I design the controllers - one controller per widget probably?
  • How should i separate the state (scope) if i have multiple widgets from the same type in the view?
  • Are there bestpractices for designing reusable widgets with AngularJS?

EDIT

Useful references:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These are just general advices.

How should I design my app and widgets - should i have a separate AngularJS module or each widget should be a directive to the main module?

You're talking hundres of widgets, it seems natural to split them into several modules. Some widgets might have more in common than other widgets. Some might be very general and fit in other projects, others are more specific.

If I design my widget as directives, is there a way to define dependency within a directive. I.e. to say that my directive uses ng-calender in its implementation?

Dependencies to other modules are done on a module level, but there is no problem if module A depends on module B and both A and B depends on module C. Directives are a natural choice for creating widgets in Angular. If a directive depends on another directive you either define them in the same module, or create the dependency on a modular level.

If I design each widget as a separate module, is there a way to dynamically add the widget module as a dependency to the main module?

I'm not sure why you would want to do this, and I'm not sure how to do it. Directives and services are not initialized before they get used in Angular. If you have a huge library of directives (widgets) and know that you'll probably use some of them, but not all of them - but you don't know which ones will get used when the application gets initialized you can actually "lazy load" your directives after your module has been loaded. I've created an example here

The benefit is that you can get your application to load fast even if you have lots of code, because you don't have to load the scripts before you need them. The disadvantage is that there can be a considerably delay the first time a new directive is loaded.

How should I design the controllers - one controller per widget probably?

A widget will probably need its own controller. Controllers should generally be small, if they get big you can consider if there's any functionality that would fit better in a service.

How should i separate the state (scope) if i have multiple widgets from the same type in the view?

Widgets that need scope variables should without doubt have their own isolated scopes (scope:{ ... } in the directive configuration).

Are there bestpractices for designing reusable widgets with AngularJS?

Isolate the scope, keep the dependencies to a necessary minimum.See Misko's video about best practices in Angular

Brian Ford has also written an article about writing a huge application in Angular


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...