GVKun编程网logo

javascript – 使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)

17

在这篇文章中,我们将带领您了解javascript–使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)的全貌,同时,我们还将为您介绍有关javascript–CKEditor打破我的

在这篇文章中,我们将带领您了解javascript – 使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)的全貌,同时,我们还将为您介绍有关javascript – CKEditor打破我的其他js应用程序,如何加载我自己的js文件、JavaScript – JS文件双重包含在rails应用程序中、javascript – node.js socket.io redis rails – 实时应用程序、javascript – Sails js:如何在Sails js的单个模型中定义两个不同数据库的MySQL连接?的知识,以帮助您更好地理解这个主题。

本文目录一览:

javascript – 使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)

javascript – 使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)

我想将特定脚本中的标记插入到特定视图中,而不是其他任何地方. This question几乎捕获了我的问题,但是我想使用我编写的脚本并希望将其放入客户端的公共目录中,而不是在其他地方实现带有hfref的脚本标记到http地址.我看到给定的解决方案如何允许您在特定视图中放置具有唯一href的脚本标记,但我不知道在哪里存储我想要插入到视图中的js,以便它将成为可用资源.我一开始以为我放在’/ assets’里面的任何目录都放在客户端的公共目录里,但是当我尝试添加’/ assets / localScripts’目录并将我的脚本放在其中时,目录,脚本也没​​出现.所以我认为我将本地脚本放在’assets / js’目录中,但我放在那里的所有东西(即使它在子目录中)都被添加到每个视图中,我不希望这个脚本被注入主页(或任何地方接受一个特定的视图).

我查看了pipeline.js文件,试图修改grunt任务. This resolved question on excluding files from grunt tasks建议我应该告诉grunt忽略文件’/assets/js/localScripts/gameview.js’,但我没有运气.我尝试添加到管道部分的末尾,选择js文件以注入以下每个异常(做同样的事情):

var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',// Dependencies like jQuery,or Angular are brought in here
  'js/dependencies/**/*.js',// All of the rest of your client-side js files
  // will be injected here in no particular order.
  'js/**/*.js',//My attempted exceptions:

  //I think this should ignore anything in the localScripts directory
  !'**/localScripts/**',//I think this should ignore any file named gameview.js
  !'**/gameview.js',//I think this should ignore '/assets/js/localScripts/gameview.js'
  !'/assets/js/localScripts/gameview.js'
];

这些异常都不起作用(在每种情况下脚本都被注入每个页面).我是在正确的轨道上吗?如何为具有为其注入脚本标记的视图提供js文件,但是不要使用文件的脚本标记自动注入每个视图?

解决方法

你想要改变你的grunt文件.要么 . ..

我的简单解决方案,我在我的应用程序中使用的是,我摆脱了自动注入脚本的布局文件中的代码.我将我的脚本放在assets / js目录(和子目录)中.然后我直接在我的文件中引用它们.

如果我想在所有视图中使用它们,那么我将它添加到我的布局文件中,如果我只想在特定页面上将其添加到该文件中.

javascript – CKEditor打破我的其他js应用程序,如何加载我自己的js文件

javascript – CKEditor打破我的其他js应用程序,如何加载我自己的js文件

这里有趣的转变.我整理了一个外部应用程序,它加载了网页,工作正常.但是,当我建立一个CKEDITOR的实例时,它会中断. Chrome会发出错误:

Uncaught TypeError: undefined is not a function

请注意,我的代码中的以下函数返回了未定义的错误:

URL.createObjectURL();

无论如何,这是我如何建立编辑器的实例.应该注意的是,如果我删除这段代码,我的js applet工作正常.

jQuery(document).on("click", "#<?=$this->c_id;?>-launch-editor-<?PHP echo $this->section_num; ?>", 
    function(){
        //contentEditor.html("");
        contentEditor.hide();
        jQuery(".editor").append('
            <div>
                <div>
                    <div>
                        Title: <?PHP echo $this->section_title; ?>
                    </div>
                    <img id="close-<?=$this->c_id;?><?PHP echo $this->section_num; ?>"src="../skins/blues/images/red-ex.png" 
                         title="Close" />
                </div>
            </div>
            <textareaid="<?PHP echo $this->c_id; ?>-<?PHP echo $this->section_num; ?>-editor" 
                      name="<?PHP echo $this->section_num; ?>-editor">
                '+innerTextArea+'
            </textarea>
        ');

        contentEditor.fadeIn(1500);

        CKEDITOR.replace('
            <?PHP echo $this->c_id; ?>-<?PHP echo $this->section_num; ?>-editor', 
            {
                toolbar : 'Full',
                width : "1020px",
                codeSnippet_theme: 'monokai_sublime'
            });

            CKEDITOR.on( 'instanceReady', function( ev )
            {
                alert("CKEditor is loaded");
            });
        });

这段代码是导致问题的原因…删除此代码允许其他一切正常工作:

CKEDITOR.replace('<?PHP echo $this->c_id; ?>-<?PHP echo $this->section_num; ?>-editor', 
{
    toolbar : 'Full',
    width : "1020px",
    codeSnippet_theme: 'monokai_sublime'
});

下面是我如何包含我的外部js文件.这是在加载ckeditor时停止工作的原因:

</table>
    <tr>
            <td></td>
            <td></td>
            <td><?PHP echo $this->section_title; ?></td>
            <td>
                <form>
                    <divid="<?=$this->c_id;?>-launch-editor-<?=$sectionNumber;?>"
                        title="Launch Content Editor"><?PHP echo $contentStatus; ?></div>

                </form>
            </td>
            <td>
                <form>
                    <divid="<?=$this->c_id;?>-launch-recorder-<?=$sectionNumber;?>"
                        title="Launch Content Recorder"><?PHP echo $recordAudio; ?></div>
                </form>
            </td>
        </tr>
    </table>
<div>
<div></div>
    <divhttps://www.jb51.cc/tag/ott/" target="_blank">ottom-container">              
        <section>        
            <div>        
                <audio id="audio" autoplay controls></audio>        
                <buttonid="record-audio">Record</button>      
                <buttonid="stop-recording-audio" disabled>Stop</button>       
                <h2 id="audio-url-preview"></h2>        
            </div>  
        </section>
    </div>
</div>
<script src="/skins/js/recordermp3.js"></script>
<script src="/skins/js/RecordRTC.js"></script>

解决方法:

对我来说,看起来你的问题可能是由于使用PHP来注入你的内容ID.如果没有您的所有代码进行试验,我会继续看似您想要的……这似乎是“在您的表中,当点击启动内容编辑器链接时,已加载THOW ROW的内容进入编辑.“由于您的PHP已经为每行设置了ID值,因此您无需在javascript中包含它们,因为您可以从被单击的表行中检索它们.您可以在行上的HTML5数据字段中省去一些麻烦,如下所示:

<td><?PHP echo $this->section_title; ?></td>
<td>
    <form>
        <divdata-cid="<?=$this->c_id;?>" 
            data-section-num="<?=$sectionNumber;?>"
            data-section-title="<?PHP echo $this->section_title; ?>"
            id="<?=$this->c_id;?>-launch-editor-<?=$sectionNumber;?>"
            title="Launch Content Editor"><?PHP echo $contentStatus; ?></div>
    </form>
</td>

然后,您可以在JavaScript中使用这些数据字段,而不必让PHP将它们注入到您的脚本中:

$(document).on("click", ".launch-content-editor", function(e){
    var launcher = $(e.target);
    var sectionTitle = launcher.data('section-title');
    var cId = launcher.data('cid');
    var sectionNum = launcher.data('section-num');

    var editorTextAreaId = cId + '-' + sectionNum + '-editor';
    var innerTextArea = $('#' + cId + '-launch-editor-' + sectionNum).html();

    var newEditor = $('<div><div><div><div>Title: ' + sectionTitle + '</div><img id="close-' + cId + sectionNum + '"src="../skins/blues/images/red-ex.png" title="Close" /></div></div><textareaid="' + editorTextAreaId + '" name="' + sectionNum + '-editor">' + innerTextArea + '</textarea></div>');

    $('.editor').append(newEditor);

    CKEDITOR.replace(editorTextAreaId, {
        toolbar : 'Full',
        width : "1020px",
        codeSnippet_theme: 'monokai_sublime'
    });
});

$(document).ready(function() {
    CKEDITOR.on( 'instanceReady', function( ev ){
        alert("CKEditor is loaded");
    });
});

此外,您应该在调用replace之前设置CKEDITOR.on()调用,或者在注册instanceReady回调之前加载编辑器.

这是小提琴:http://jsfiddle.net/ot6tkgdp/4/

JavaScript – JS文件双重包含在rails应用程序中

JavaScript – JS文件双重包含在rails应用程序中

我有几个rails 3.1应用程序,我在所有这些问题.

我有一堆包含在我的application.js文件中的js文件.这是我的application.js:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_self
//= require_tree .

问题是,当.development.rb,我的config.assets.debug设置为true时,我的所有javascript文件都被包含在标题中.实际上,它们没有被明确的双重包含,而是它们被单独包含,一次作为编译的application.js文件的一部分. js包含标签如下所示:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<script src="/assets/animation_elements.js?body=1" type="text/javascript"></script>
<script src="/assets/categories.js?body=1" type="text/javascript"></script>
<script src="/assets/faceBox.js?body=1" type="text/javascript"></script>
…

那么什么鬼?这样做的结果是各种JQuery onClick()行为正在执行两次,而其他废话.这里有什么问题?为了记录,这是我的整个development.rb文件:

Geobooks::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  #config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  #for devise
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = false
end

解决方法

听起来好像你可能已经在某个时候预编译了你的资产.检查正在开发中提供的application.js文件的内容.我猜你会看到所有其他所需文件的捆绑内容.要解决这个问题,只需删除/public/assets/javascripts/application.js并弹出你的服务器.

如果这不起作用,找出第二个副本来自哪里(使用Firebug或类似的方式检查提供的文件)并发布更新,我将看到我能做什么.

javascript – node.js socket.io redis rails – 实时应用程序

javascript – node.js socket.io redis rails – 实时应用程序

我需要在我的应用程序( Ruby On Rails)中添加实时,因此,我认为更好的方法是使用node.js socket.io redis.

我在后端有这个application.js文件(node.js)

var app = require('http').createServer();
var io = require('socket.io');
var redis = require('redis').createClient();
var _ = require('underscore')._;

io = io.listen(app);
io.configure(function() {
    io.set("transports",["xhr-polling"]);
    io.set("polling duration",10);
    io.set("close timeout",10);
    io.set("log level",1);
})

redis.subscribe('rt-change');

io.on('connection',function(socket) {
    redis.on('message',function(channel,message) {
        socket.emit('rt-change',message)
    });
});

var port = process.env.PORT || 5001;
app.listen(port);

和前端的messages.js

var socket = io.connect('http://localhost:5001/socket.io');
socket.on('rt-change',function (data) {
    console.log(data);
});

我正在使用node application.js命令启动application.js,它可以工作!

MacBook-Pro-Zhirayr:rt zhirayr$node application.js info –
socket.io started

但是当我尝试从Rails应用程序发送带有redis的消息($redis.publish’rt-change’,{hello:’world’})时,我的浏览器不会在控制台中记录任何内容.
我确定,从浏览器建立的连接,导致我停止节点时,它会抛出连接拒绝错误.我确信redis和node之间建立了连接,导致application.js中的console.log(message)记录它.
但是浏览器中的console.log不会记录任何内容.

有什么想法吗?

谢谢.

UPD for #Antoine

在application.js中添加了console.log
io.on(‘connection’,function(socket){
redis.on(‘message’,message){
console.log(‘来自redis的新消息’);
socket.emit(‘rt-change’,message);
});
});

当r.publish’rt-change’,{:hello =>’world’}被执行时,节点记录下:

new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis

奇怪的是,1个消息的节点记录11次.

解决方法

var socket = io.connect('http://localhost:5001/socket.io');
socket.on('rt-change',function (data) {
    console.log(data);
});

根据http://socket.io上的文档,这部分代码似乎不正确,
你应该做的事情如下:

<script src="http://localhost:5001/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:5001');
</script>

javascript – Sails js:如何在Sails js的单个模型中定义两个不同数据库的MySQL连接?

javascript – Sails js:如何在Sails js的单个模型中定义两个不同数据库的MySQL连接?

我创建了一个模型Employee.js和EmployeeController.js.我在Employee.js文件中定义了两个连接:

    module.exports = {

    connection: ''LocalhostMysqLServer'',
    attributes: {    
      name:{
        type:"string", 
        required:true,
      },
      empnum:{
        type:"integer",
        required:true,
        unique: true
      },
      email:{
        type:"string",
        required:true,
        unique: true
      }  
    },

    connection: ''LocalhostMysqLServer1'',
      attributes: {
        username:{
        type:"string", 
        required:true,
      },
      usrnum:{
        type:"integer",
        required:true,
        unique: true
      },
      email:{
        type:"string",
        required:true,
        unique: true
      }
    },
 };

下面是我的EmployeeController.js文件,其中包含两个视图:CreateEmp& CreateUsr,与此模型和控制器关联.此外,我已经定义了两个函数来处理来自这些视图的post请求.在这里,我想将CreateEmp中的数据插入到不同的数据库中,并将CreateUsr中的数据插入到不同的数据库中.

    module.exports = {

      createEmp: function(req,res){
        ''use strict'';
        res.view(''new.ejs'');
      },

      createUsr: function(req,res){
        ''use strict'';
        res.view(''newUser.ejs'');
      },

      createEmployee: function(req, res){
        if(req.method=="POST"){
          var name= req.param("name");
          var empnum= req.param("empnum");
          var email= req.param("email");

          var insert= "INSERT INTO employee(name, empnum, email) VALUES (''"+name+"'', "+empnum+", ''"+email+"'')";
          Employee.query(insert, function(err, record){
            if(err)
              console.log(err);
            else{
              console.log(record);
            }
          })
        }
      },

      createuser: function(req, res){
        if(req.method=="POST"){
          var username= req.param("username");
          var usrnum= req.param("usrnum");
          var email= req.param("email");

          var insert= "INSERT INTO user (username, usrnum, email) VALUES (''"+username+"'', "+usrnum+", ''"+email+"'')";
          Employee.query(insert, function(err, record){
            if(err)
              console.log(err);
            else{
              console.log(record);
            }
          })
        }
      },

    };

我在这里包含了我的config / connections.js:

    module.exports.connections = {

      localdiskDb: {
        adapter: ''sails-disk''
      },


      LocalhostMysqLServer: {
        adapter: ''sails-MysqL'',
        //module: ''sails-MysqL'',
        host: ''localhost'',
        user: ''root'',
        password: ''disisanshu'',
        database: ''sailsTest1'',
        tableName: ''employee''
      },

      LocalhostMysqLServer1: {
        adapter: ''sails-MysqL'',
        host: ''localhost'',
        user: ''root'',
        password: ''disisanshu'',
        database: ''sailsTest2'',
        tableName: ''user''
      }

    };

这里我在下面包含了我的model.js:

    module.exports.models = {


      // migrate: ''alter''

      connection: ''LocalhostMysqLServer'',
      migrate: ''alter'',
      connection1: ''LocalhostMysqLServer1'',
      migrate: ''alter''

    };

解决方法:

没有理由你应该做这样的事情.

您可能希望减少数据库的负载.应该实现主从数据库类型的结构.

此外,数据库应与您的应用程序的其余部分完全分离.这就是您应该只有一个连接的原因.
如果数据库上的负载增加,请将其水平扩展(添加更多服务器以分配负载) – MysqL适用于这些事情.这可以而且应该在您的应用代码没有任何变化的情况下完成.

总结

以上是小编为你收集整理的javascript – Sails js:如何在Sails js的单个模型中定义两个不同数据库的MySQL连接?全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

原文地址:https://codeday.me/bug/20190628/1314669.html

关于javascript – 使js文件在sails应用程序中可用资源(不将它们注入到每个视图中)的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于javascript – CKEditor打破我的其他js应用程序,如何加载我自己的js文件、JavaScript – JS文件双重包含在rails应用程序中、javascript – node.js socket.io redis rails – 实时应用程序、javascript – Sails js:如何在Sails js的单个模型中定义两个不同数据库的MySQL连接?等相关知识的信息别忘了在本站进行查找喔。

本文标签: