Tag: underscore.js

从Array中删除空对象

我有一个填充了对象的JavaScript数组,并希望删除没有数据的每个对象。 它可能看起来像这样: var myArray = [ {id: “28b”, text:”Phill”}, {id: “12c”, text:”Peter”}, {id: “43f”, text:”Ashley”}, {id: “43f”, text:”Ashley”}, {id: “”, text:””}, {id: “9a”, text:”James”}, {id: “”, text:””}, {id: “28b”, text:”Phill”} ]; 我已经使用_.uniq来自underscore.js的_.uniq来删除我的数组中的所有重复项,这很正常。 虽然它们是唯一的,但是当我动态填充数据时,总是会留下一个空对象(因为有空数据集)。 我已经尝试过这里提到的_.without函数: 在Javascript中从数组中删除空元素但它不起作用。 这是我的尝试: myArray = _.without(myArray, {id:””,text:””}); 该数组应如下所示: [ {id: “28b”, text:”Phill”}, {id: “12c”, text:”Peter”}, {id: “43f”, text:”Ashley”}, {id: “9a”, text:”James”}, ]; […]

Backbone.js:执行Fetch()后,只渲染新模型

我有一个Collection App.listingList ,其中使用add:true调用后续的fetch() 。 App.listingList.fetch({ data: {some:data}, processData: true, add: true }); 问题:新添加的模型如何渲染其视图,而无需重新渲染现有模型的视图。 这意味着我做不到: this.collection.each( function(listing, index) { new ListingMarkerView({ model:listing }).render(); }, this); 尝试#1 在集合的add事件上渲染View,我无法找到一种方法来访问要渲染的新模型 ListingListView = Backbone.View.extend({ initialize: function() { this.collection.bind(‘add’, this.renderNew, this); }, render: function() { console.log(‘render ListingListView’); this.collection.each( function(listing, index) { new ListingMarkerView({ model:listing }).render(); }, this); return this; }, renderNew: […]

JavaScript错误:“不是构造函数”

我正在使用backbone.js以及jquery和underscore.js 这是我的一些代码(它还没有做任何事情)。 奇怪的是,在点击“/#users”时,没有错误。 错误发生的唯一时间是我然后单击转到另一个哈希,然后单击返回“/#users”。 这是我的代码的一部分,用于向最终Users = new Users();接收错误的行Users = new Users(); 错误说“用户不是构造函数”: var User = Backbone.Model.extend({ url: function(){return ‘api/user/id/’ + this.id;} }); var Users = Backbone.Collection.extend({ model: User, url: function(){return ‘api/users’;}, initialize: function() { } }); var UsersView = Backbone.View.extend({ initialize: function() { this.users = this.options.users; this.render(); }, render: function() { _(this.users).each(function(){ // $(‘#app’).append(‘here’); }); […]

将查询字符串反序列化为JSON对象

试图找到如何使{foo:”bar”}来自?…&foo=bar&…但谷歌搜索并只得到jQuery.params相反。 任何建议请(内置的javascript函数, jquery , underscore.js – 一切顺利)? 或者,我是否需要自己实施(不是很麻烦,只是试图不重新发明轮子)?

Rails 4 – 具有简单forms的依赖字段的JS

我想在Rails 4中创建一个应用程序。 我正在使用简单的表单forms,并且刚刚尝试使用gem’inpendent-fields-rails’来隐藏或显示基于主要问题的表单字段的子集问题。 我卡住了。 我已将gems添加到我的gem文件中: gem ‘dependent-fields-rails’ gem ‘underscore-rails’ 我已将我的application.js更新为: //= require dependent-fields //= require underscore 我的表格有: “Is this a project in which students may participate?”, autofocus: true %> “Is this a project students complete for credit towards course assessment?” %> “Is this project offered on a recurring basis?” %> “How often is this project […]

Backbone.js Pushstate Routing和PHP Framework的路由器

我正在使用Backbone.js和PHP MVC框架(Codeigniter,Laravel)。 我对使用Pushstates时PHP路由器和Backbone路由器如何处理URL感到困惑。 (我还没有开始使用pushstates,仍然使用hashbangs # )。 我不是在单页应用程序上工作,而是该站点由几个页面组成,这些页面本身就像单页面应用程序。 问题:如果我有一个页面http://domain.com/user/user123 ,并点击链接http://domain.com/user/user123#photos通过AJAX加载一些照片,我想摆脱# ,我预见到用户在点击链接后访问http://domain.com/user/user123/photos决定将URL复制并粘贴到另一个浏览器选项卡中的问题,将会看到错误404页面作为PHP框架路由器(如果我正确地假设PHP路由器在骨干网之前首先处理URL请求)不能识别此URL的存在。 该问题应如何解决? 我认为会出现的另一个问题是当用户直接访问http://domain.com/user/user123/photos时应该如何呈现视图? 是否应在PHP视图(对于用户直接输入URL的实例)和backbone.js视图(对于用户从http://domain.com/user/user123上单击链接的情况)复制HTML代码http://domain.com/user/user123 )? 这对我来说似乎不是最佳解决方案。 或者可以通过骨干在两种情况下以某种方式呈现View以避免重复代码? 请指教 :)

从Ul骨干中移除li

我试图建立一个非常简单的ul,你有一个输入框和添加按钮,点击添加按钮,输入的文本被附加到ul 这是我的代码: HTML: Add JS: $(function(){ var myCollection = Backbone.Collection.extend(); var myView = Backbone.View.extend({ el:$(‘body’), tagName:’li’, initialize : function(e){ this.collection.bind(“add”,this.render,this); }, events:{ ‘click #add’ : ‘addfoo’, ‘click .button’:’removefoo’ }, addfoo:function(){ var myname= $(‘#name’).val(); $(‘#name’).val(”); console.log(‘name entered: ‘+myname); this.collection.add({name:myname}); }, removefoo:function(){ //need the code here }, render:function(){ $(‘#mylist’).empty(); this.collection.each(function(model){ $(‘#mylist’).append(”+model.get(‘name’) + “”+”delete”+””); }); } }); var […]

无法在backbonejs中的集合上使用create方法

我有一个骨干集合,如下所示: app.ledgerList=Backbone.Collection.extend({ model: app.ledger, localStorage:new Store(‘ledgers’) }); ///initializing app.ledgers=new app.ledgerList(); 在我以后的应用程序中的一个观点中,我有: saveLedger: function(){ name=$(“#ledgerName”).val(); email=$(“#ledgerEmail”).val(); address=$(“#ledgerAddress”).val(); phone=$(“#ledgerPhone”).val(); console.log(app.ledgers.length); app.ledgers.create({name:name,email:email,address:address,phone:phone}); } 这将返回我Uncaught TypeError: Object [object Object] has no method ‘apply’在app.ledgers.create({name:name,email:email,address:address,phone:phone})行上Uncaught TypeError: Object [object Object] has no method ‘apply’ app.ledgers.create({name:name,email:email,address:address,phone:phone})可能是什么问题?

IBM Worklight 6.1 – 未捕获的ReferenceErrors:未定义WLJQ,未定义WL

我正在使用Eclipse Juno中安装的IBM Worklight Developer Edition 6.1作为我的IDE和backbonejs作为我项目的主要javascript框架。 我的index.html <!– window.$ = window.jQuery = WLJQ; 我的main.js require.config({ paths: { jquery: ‘libs/jquery/jquery-min’, underscore: ‘libs/underscore/underscore-min’, backbone: ‘libs/backbone/backbone-min’, templates: ‘../templates’, bootstrap: ‘bootstrap.min’, initOptions: ‘initOptions’, biiRibMobile: ‘biiRibMobile’, messages: ‘messages’ }, }); require([‘app’, ], function(App) { App.initialize(); }); 我的app.js. // Filename: app.js define([‘jquery’, ‘underscore’, ‘backbone’, ‘router’, // Request router.js ‘bootstrap’, ‘initOptions’, ‘biiRibMobile’, […]

在backbone.js中绑定和_.bindAll

我对_bind.All中绑定和_bind.All的目的感到困惑。 下面是一个工作代码,它创建一个模态视图#modal并呈现从后端获取的注释。 首先,在下面的代码中,我有initialize函数_.bindAll(this, ‘render’, ‘renderComments’); 。 无论我是否_.bindAll() ,我都可以在initialize()调用this.render()和this.renderComments() initialize() 。 是否有任何关于_.bindAll()何时会帮助我们以及何时不会帮助我们的例子? ModalView = Backbone.View.extend({ el: $(‘#modal’), template: _.template( $(‘#tpl_modal’).html() ), initialize: function() { _.bindAll(this, ‘render’, ‘renderComments’); this.render(); this.renderComments(); }, render: function() { $(this.el).fadeIn(‘fast’).append( this.template( this.model.toJSON( this.model ) ) ); }, renderComments: function() { this.commentList = new CommentCollection(); var self = this; this.commentList.fetch({ data: { post_id: […]