Question

I am fairly new to snippet creation. I have successfully created multiple snippets for C# and HTML. When I ran into troubles I started using the MS Snippet Designer. The trouble I had was when I created a snippet for JS using the following code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>tjready</Title>
      <Author>admin</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>$().ready(function() {</Shortcut>
    </Header>
    <Snippet>
      <Code Language="javascript"><![CDATA[$().ready(function() {
//This is a test of the onready
    });]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

My goal is to create the snippet for either JS or CSHTML files. The above does not work in either type of file after an import using the Code Snippet Manager (Tools > CSM). I tried placing the snippet in both the html and JavaScript categories. I also tried modifying the language type of the snippet.

Does anyone see what I am doing wrong?

Was it helpful?

Solution

For anyone else that comes across this as well, if you need to add a leading $, add two.

<Snippet>
  <Code Language="javascript"><![CDATA[$$().ready(function() {//This is a test of the onready    });]]></Code>
</Snippet>

OTHER TIPS

I found the issue and a solution. The issue was the leading $. Once I took that off I could create the snippit using ctrl K + ctrl X. I wanted to use the shortcut type and auto fill though.

Since I could not get the type and tab auto fill to work I continued to look. I found a Visual Studio jQuery snippet plugin that works for what I am trying to do and comes with several snippets ready to go.

The jQuery snippet shortcut on this is jqDocReady or you can use jqDocReadyShort if you do not want the anonymous function in there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top