javascript

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package javascript converts go objects and types to javascript code, suitable for embedding in html or sending to the browser via a specialized ajax call.

Index

Examples

Constants

View Source
const JsonObjectType = "goraddObject"

JsonObjectType is used by the ajax processor in goradd.js to indicate that we are sending a special kind of object to the browser. These are things like dates, closures, etc. that are not easily represented by JSON.

Variables

This section is empty.

Functions

func NumberFloat

func NumberFloat(i interface{}) float64

NumberFloat is a helper function to convert an expected float that is returned from a json Unmarshal as a Number, into an actual float64 without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a float. Can convert strings too.

func NumberInt

func NumberInt(i interface{}) int

NumberInt is a helper function to convert an expected integer that is returned from a json Unmarshal as a Number, into an actual integer without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting an integer. Can convert strings too.

func NumberString

func NumberString(i interface{}) string

NumberString is a helper function to convert a value that might get cast as a Json Number into a string. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a string.

func ToJavaScript

func ToJavaScript(v interface{}) string

ToJavaScript will convert the given value to javascript such that it can be embedded in a browser. If it can, it will use the JavaScripter interface to do the conversion. Otherwise it generally follows json encoding rules. Strings are escaped. Nil pointers become null objects. String maps become javascript objects. To convert a fairly complex object, like a map or slice of objects, convert the inner objects to interfaces

Types

type Arguments

type Arguments []interface{}

Arguments represents a list of javascript function arguments. We can output this as javascript, or as JSON, which gets sent to the goradd javascript during Ajax calls and unpacked there. Primitive types get expressed as constant values in javascript. If you want to represent the name of variable, us a JsCode object. Function can be represented using the Function object or the Closure object, depending on whether you want the output of the function now, or later.

func (Arguments) JavaScript

func (a Arguments) JavaScript() string

JavaScript returns the arguments as a comma separated list of values suitable to put in JavaScript function arguments.

type JavaScripter

type JavaScripter interface {
	JavaScript() string
}

JavaScripter specifies that an object can be converted to javascript (not JSON!). These objects should also be gob encodable and registered with gob, since they might be embedded in a control and need to be serialized.

type Undefined

type Undefined struct {
}

Undefined explicitly outputs as "undefined" in javascript. Generally, nil pointers become "null" in javascript, so use this if you would rather have an undefined value.

func (Undefined) JavaScript

func (n Undefined) JavaScript() string

func (Undefined) MarshalJSON

func (n Undefined) MarshalJSON() ([]byte, error)

type Ωclosure added in v0.0.3

type Ωclosure struct {
	// Body is the body javascript of the closure
	Body string
	// Args are the names of the arguments in the argument list of the closure
	Args []string
}

func Closure

func Closure(body string, args ...string) Ωclosure

Closure represents a javascript function pointer that can be called by javascript at a later time.

Example
c := Closure("return a == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

function(a, b) {return a == b;}

func (Ωclosure) JavaScript added in v0.0.3

func (c Ωclosure) JavaScript() string

JavaScript implements the JavsScripter interface and returns the closure as javascript code.

func (Ωclosure) MarshalJSON added in v0.0.3

func (c Ωclosure) MarshalJSON() (buf []byte, err error)

MarshalJSON implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd javascript file during Ajax calls.

type ΩclosureCall added in v0.0.3

type ΩclosureCall struct {
	// Body is the body javascript of the closure
	Body string
	// Context is what will become the "this" var inside of the closure when called. Specifying "this" will bring the "this" from the outer context in to the closure.
	Context string
	// Args are the names of the arguments in the argument list of the closure
	Args []string
}

func ClosureCall

func ClosureCall(body string, context string, args ...string) ΩclosureCall

ClosureCall represents the result of a javascript closure that is called immediately context will become the "this" variable inside the closure when called.

Example
c := ClosureCall("return this == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

(function(b) {return this == b;}).call(a)

func (ΩclosureCall) JavaScript added in v0.0.3

func (c ΩclosureCall) JavaScript() string

JavaScript implements the JavsScripter interface and returns the closure as javascript code.

func (ΩclosureCall) MarshalJSON added in v0.0.3

func (c ΩclosureCall) MarshalJSON() (buf []byte, err error)

Implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd javascript file.

type Ωfunction added in v0.0.3

type Ωfunction struct {
	// The function name
	Name string
	// If given, the object in the window object which contains the function and is the context for the function.
	// Use dot '.' notation to traverse the object tree. i.e. "obj1.obj2" refers to window.obj1.obj2 in javascript
	Context string
	// Function arguments. Strings will be quoted. Use a JsCode object to output the name of a javascript variable.
	Args []interface{}
}

func Function

func Function(name string, context string, args ...interface{}) Ωfunction

Function represents the result of a function call to a global function or function in an object referenced from global space. The purpose of this is to immediately use the results of the function call, as opposed to a Closure, which stores a pointer to a function that is used later. context will become the "this" value inside the closure args will be passed as values, and strings will be quoted. To pass a variable name, wrap the name with a JsCode call.

func (Ωfunction) JavaScript added in v0.0.3

func (f Ωfunction) JavaScript() string

func (Ωfunction) MarshalJSON added in v0.0.3

func (f Ωfunction) MarshalJSON() (buf []byte, err error)

*

  • Returns this as a json object to be sent to qcubed.js during ajax drawing.
  • @return mixed

type ΩjsCode

type ΩjsCode struct {
	Code  string
	IsInt bool
}

func JsCode

func JsCode(s string) ΩjsCode

JsCode represents straight javascript code that should not be escaped or quoted. Normally, string values would be quoted. This outputs a string without quoting or escaping.

func (ΩjsCode) JavaScript

func (c ΩjsCode) JavaScript() string

type ΩnoQuoteKey added in v0.0.3

type ΩnoQuoteKey struct {
	Value interface{}
}

func NoQuoteKey

func NoQuoteKey(v interface{}) ΩnoQuoteKey

NoQuoteKey is a value wrapper to specify a value in a map whose key should not be quoted when converting to javascript. In some situations, a quoted key has a different meaning from a non-quoted key. For example, when making a list of parameters to pass when calling the jQuery $() command, (i.e. $j(selector, params)), quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object. i.e. map[string]string {"size":4, "size":NoQuoteKey(JsCode("obj"))}

func (ΩnoQuoteKey) JavaScript added in v0.0.3

func (n ΩnoQuoteKey) JavaScript() string

Prevent using this as a general value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL