在所有全局 javascripts 之后将 javascript 注入 Sailsjs 中的特定视图/路由 [英] Inject javascript to specific view/route in Sailsjs after all the global javascripts

查看:33
本文介绍了在所有全局 javascripts 之后将 javascript 注入 Sailsjs 中的特定视图/路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在尝试使用sailsjs构建一个非常简单的聊天应用程序,该应用程序只有3个页面,每个页面都有不同的参考数据集,这些数据将提供给twitter typeahead js,我打算做的是这样的:

Recently I have been trying to build a really simple chat app using sailsjs, the app has only 3 pages, each page has different sets of reference data that will feed twitter typeahead js, what I intend to do is like this:

var socket = io.connect('http://localhost:1337');

$(document).ready(function () {
    var $modalLayer = $(JST['assets/templates/generic/modal.html']({}));
    $modalLayer.appendTo("body").modal('show');

    socket.on('connect', function () {
        socket.request('/chat/references', {}, function (data) {
            console.log(data);
            bindTypeAhead(data);
            $modalLayer.modal('hide');
    });

});

对于不同的页面我有不同的逻辑,所以需要分成不同的javascript文件.

For different page I have different logic, so need to be separated into different javascript files.

我尝试过的是:

  • 在 pagea.ejs 和 pageb.ejs 中使用标签
  • 使用 <%- block('localScripts', 'SCRIPT_TAG') %>

这两种方法都使脚本出现在 pipeline.js 中定义的所有 javascript 注入之前,因此 socket.io 变得未定义.

Both approaches made the script come before all the javascript injections defined in pipeline.js, thus socket.io becomes undefined.

我在stackoverflow上找到的一些解决方案是将javascript注入移动到<head>",但我认为这是一种hacky方式,我真的不想打破sailsjs约定.

Some solutions I found on stackoverflow I read was to move the javascript injections to "<head>", but I think this is a hacky way to go, I dont really want to break sailsjs convention.

谁能有好的方法吗?

非常感谢.

我使用的帆是 0.10.5 版本.

The sails I'm using is versioned 0.10.5.

推荐答案

在我的项目中,我使用一个布局模板和一个 <%-scripts%> 标签来指定子页面脚本应该放在哪里.

In my project I use a layout template and a <%-scripts%> tag to designate where child pages scripts should go.

这篇关于在所有全局 javascripts 之后将 javascript 注入 Sailsjs 中的特定视图/路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆