Inject javascript to specific view/route in Sailsjs after all the global javascripts

二次信任 提交于 2019-12-11 11:35:07

问题


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');
    });

});

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

What I have tried were:

  • using tag in pagea.ejs and pageb.ejs
  • using <%- block('localScripts', 'SCRIPT_TAG') %>

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

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.

Could anybody have a nice way of doing it?

Many thanks.

The sails I'm using is versioned 0.10.5.


回答1:


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



来源:https://stackoverflow.com/questions/27120430/inject-javascript-to-specific-view-route-in-sailsjs-after-all-the-global-javascr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!