Permalink
Browse files

优化为实例的方式

  • Loading branch information...
1 parent 7cbc0be commit ca48634aed81d6ac0513bf64877180d4f8b6e656 @dantezhu committed Oct 22, 2012
Showing with 66 additions and 31 deletions.
  1. +3 −3 README.md
  2. +3 −3 example/main.py
  3. +4 −1 example/templates/index.html
  4. +55 −23 flask_util_js.py
  5. +1 −1 setup.py
View
6 README.md
@@ -9,16 +9,16 @@ flask's util in javascript. such as url_for etc.
from flask import Flask
- import flask_util_js
+ from flask_util_js import FlaskUtilJs
app = Flask(__name__)
- flask_util_js.install(app)
+ fujs = FlaskUtilJs(app)
###### load flask_util.js in your html file
- <script src="{{ url_for('flask_util_js') }}" type="text/javascript" charset="utf-8"></script>
+ <script src="{{ fujs.path }}" type="text/javascript" charset="utf-8"></script>
###### use url_for in your js file
View
6 example/main.py
@@ -7,16 +7,16 @@
from flask import Flask, Blueprint
from flask import render_template
-import flask_util_js
+from flask_util_js import FlaskUtilJs
app = Flask(__name__)
app.config.from_object(__name__)
-flask_util_js.install(app)
+fujs = FlaskUtilJs(app)
@app.route('/')
def index():
- return render_template('index.html')
+ return render_template('index.html', fujs=fujs)
@app.route('/<int:myid>')
def show_id():
View
5 example/templates/index.html
@@ -6,7 +6,7 @@
<title>Index</title>
- <script src="{{ url_for('flask_util_js') }}" type="text/javascript" charset="utf-8"></script>
+ <script src="{{ fujs.path }}" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var url = flask_util.url_for('sub.bpt_index', {y:2, x:'/sdf'});
@@ -18,6 +18,9 @@
</head>
<body>
+ <p>flask_util.js:</p>
+ <p>path: {{ fujs.path }}</p>
+ <p>endpoint: {{ fujs.endpoint }}</p>
</body>
</html>
View
78 flask_util_js.py
@@ -15,10 +15,11 @@
# HomePage: http://www.vimer.cn
#
# Created: 2012-07-09 17:23:51
-# Version: 0.1
+# Version: 0.2.0
# History:
# 0.0.1 | dantezhu | 2012-07-09 17:23:51 | initialization
# 0.1 | dantezhu | 2012-08-30 22:54:33 | 正式版本
+# 0.2.0 | John Doe | 2012-10-22 21:53:14 | 优化为实例的方式
#
#=============================================================================
'''
@@ -119,31 +120,62 @@
{% endautoescape %}
'''
-def install(app):
- """
- 安装到app上
- """
- path = app.config.get('FLASK_UTIL_JS_PATH', FLASK_UTIL_JS_PATH)
- endpoint = app.config.get('FLASK_UTIL_JS_ENDPOINT', None)
+class FlaskUtilJs(object):
+ """FlaskUtilJs"""
- @app.route(path, endpoint=endpoint)
- def flask_util_js():
- org_url_map = app.url_map._rules_by_endpoint
+ def __init__(self, app=None):
+ """init with app
- #把重的逻辑还是放到python代码里
- url_map = dict()
+ :app: Flask instance
- for k,v in org_url_map.items():
- url_map[k] = dict(
- rule=v[0].rule,
- defaults=v[0].defaults or {},
- )
+ """
+ self._app = None
+
+ if app:
+ self.init_app(app)
+
+ def init_app(self, app):
+ """
+ 安装到app上
+ """
+ if self._app is not None:
+ raise Exception('Flask-Admin is already associated with an application.')
+
+ self._app = app
+
+ path = app.config.get('FLASK_UTIL_JS_PATH', FLASK_UTIL_JS_PATH)
+ endpoint = app.config.get('FLASK_UTIL_JS_ENDPOINT', None)
+
+ @app.route(path, endpoint=endpoint)
+ def flask_util_js():
+ org_url_map = app.url_map._rules_by_endpoint
- json_url_map = json.dumps(url_map, indent=4, ensure_ascii=False)
+ #把重的逻辑还是放到python代码里
+ url_map = dict()
- rv = render_template_string(
- FLASK_UTIL_JS_TPL_STRING,
- json_url_map=json_url_map
- )
+ for k,v in org_url_map.items():
+ url_map[k] = dict(
+ rule=v[0].rule,
+ defaults=v[0].defaults or {},
+ )
+
+ json_url_map = json.dumps(url_map, indent=4, ensure_ascii=False)
+
+ rv = render_template_string(
+ FLASK_UTIL_JS_TPL_STRING,
+ json_url_map=json_url_map
+ )
- return Response(rv, content_type='application/x-javascript')
+ return Response(rv, content_type='application/x-javascript')
+
+ # 最后把数据写到实例里
+ self._path = path
+ self._endpoint = endpoint or flask_util_js.__name__
+
+ @property
+ def path(self):
+ return self._path
+
+ @property
+ def endpoint(self):
+ return self._endpoint
View
2 setup.py
@@ -2,7 +2,7 @@
setup(
name="flask_util_js",
- version="0.1.1",
+ version="0.2.0",
zip_safe=False,
platforms='any',
py_modules=['flask_util_js'],

0 comments on commit ca48634

Please sign in to comment.