Example #1
0
        /// <summary>
        /// Render a partial view with AngularJS components.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="viewName">The view name.</param>
        /// <param name="model">The model.</param>
        /// <param name="viewDataDictionary">The view data dictionary.</param>
        /// <param name="ngJsDeps">The Angular JS dependencies related to this view.</param>
        /// <returns>The HTML string to print.</returns>
        /// <seealso cref="PartialExtensions.Partial(HtmlHelper, string, object, ViewDataDictionary)"/>
        public static MvcHtmlString PartialNgJs(
            this HtmlHelper htmlHelper,
            string viewName,
            object model,
            ViewDataDictionary viewDataDictionary,
            NgJsViewDeps ngJsDeps = null)
        {
            string       sanitizedViewName = GetSanitizedNameFrom(viewName);
            NgJsViewData viewData          = NgJsContext.Current.Provider.GetViewData(sanitizedViewName);

            NgJsViewDeps actualNgDeps = new NgJsViewDeps();

            NgJsViewDependenciesContext.Current.Dependencies.Add(sanitizedViewName, actualNgDeps);
            actualNgDeps.Merge(ngJsDeps);
            LoadView(actualNgDeps, viewData);

            string controllerName = viewData.JsName;
            string formName       = sanitizedViewName + "Frm"; // FIXME: Use proper init from provider

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(@"<div ng-controller=""{0} as _context"" ng-form=""{1}"">", controllerName, formName);
            sb.AppendLine(htmlHelper.Partial(viewData.TemplatePath, model, viewDataDictionary).ToHtmlString());
            sb.AppendLine("</div>");
            return(new MvcHtmlString(sb.ToString()));
        }
Example #2
0
        private void RegisterScopeInit(StringBuilder script, Dictionary <string, NgJsViewDeps> dependencies)
        {
            script.AppendLine();
            script.AppendLine("/** Register init functions **/");

            foreach (string viewName in dependencies.Keys)
            {
                NgJsViewData viewData = Provider.GetViewData(viewName);

                script.AppendFormat("if (typeof {0} == 'function') {{", viewData.InitFunctionJsName);
                script.AppendFormat("app.run({0});", viewData.InitFunctionJsName);
                script.AppendLine("}");
            }

            script.AppendLine("/** END Register init functions **/");
            script.AppendLine();
        }
Example #3
0
        private static void LoadView(NgJsViewDeps target, NgJsViewData viewData)
        {
            if (target.View != null)
            {
                throw new ArgumentException("Unable to load " + viewData.JsName + ": view has already been loaded!");
            }

            string diskPath = NgJsHelper.MapPath(viewData.Path);

            target.View = "/** INCLUDE JS " + viewData.JsName + " @" + diskPath + "**/";

            if (File.Exists(diskPath))
            {
                target.View += File.ReadAllText(diskPath);
            }
            else
            {
                target.View = "/** js at '" + diskPath + "' not found **/";
            }
        }
Example #4
0
        private void RegisterViewControllers(StringBuilder script, Dictionary <string, NgJsViewDeps> dependencies)
        {
            script.AppendLine();
            script.AppendLine("/** Register view controllers **/");

            foreach (string c in dependencies.Keys)
            {
                NgJsViewDeps dep = dependencies[c];

                if (!string.IsNullOrEmpty(dep.View))
                {
                    NgJsViewData viewData = Provider.GetViewData(c);

                    script.AppendFormat("app.controller(\"{0}\", {0}); ", viewData.JsName);
                    script.AppendLine();
                }
            }

            script.AppendLine("/** End Register view controllers **/");
            script.AppendLine();
        }